/

How to Update All npm Packages in Multiple Projects Situated in Subfolders

How to Update All npm Packages in Multiple Projects Situated in Subfolders

In this blog post, I will guide you on how to update all npm packages in multiple projects that are located within subfolders. Updating packages is an essential task to ensure your projects utilize the latest features, bug fixes, and security patches. To simplify this process, we will make use of a shell script.

  1. Create a new shell script file:

Open a text editor and create a new file. Save it with a .sh extension, for example, update_packages.sh.

  1. Begin the script:

Start by adding the following line at the beginning of the shell script file:

1
#!/bin/bash

This line specifies that the script should be run with the Bash shell.

  1. Loop through the subfolders:

To update packages in multiple projects residing in subfolders, we need to iterate through each subfolder. Add the following code snippet to the shell script file:

1
2
for dir in */; do
cd "$dir"

This code loops through each subfolder within the current directory and navigates into it.

  1. Check for the package.json file:

Next, we need to verify if the subfolder contains a package.json file, as it is an indication that the project is an npm package. Include the following code inside the loop:

1
if [ -f package.json ]; then

This condition checks if the package.json file exists in the subfolder.

  1. Update the packages:

To update the npm packages in the project, we will use two npm commands: ncu and npm update. ncu is a tool that checks for available updates in the package.json file, while npm update installs the latest versions. Add the following lines to the script:

1
2
3
rm -rf node_modules
npx ncu -u
npm update

The first line removes the existing node_modules folder to ensure a clean update. The second line uses ncu to update the package.json file with the latest package versions. The third line executes npm update to install the updated packages.

  1. Exit the subfolder:

After updating the packages in the subfolder, we need to go back to the parent directory. Insert the following line:

1
cd ..

This command navigates back to the parent directory.

  1. Finish the loop and save the script:

Add the closing line for the loop:

1
done

Save the shell script file.

  1. Make the shell script executable:

To run the shell script, we need to make it executable. In the terminal, navigate to the directory containing the shell script file and execute the following command:

1
chmod +x update_packages.sh

This command grants execute permissions to the shell script.

  1. Execute the shell script:

To update npm packages in all projects within subfolders, run the script by entering the following command in the terminal:

1
./update_packages.sh

The script will loop through each subfolder, check for the presence of a package.json file, update the packages, and navigate back to the parent directory.

This shell script simplifies the process of updating npm packages across multiple projects. By utilizing it, you can ensure that all your projects are using the latest package versions, enhancing their functionality and security.

Tags: npm, shell script, package.json, update packages