220 words
1 minutes
How to update all npm packages in multiple projects that sit in subfolders

How to update all npm packages in multiple projects that sit in subfolders#

Managing multiple Node.js projects can be challenging, especially when it comes to keeping all npm packages up to date. This guide provides a shell script to update all npm packages in multiple projects located in subfolders.

Shell Script#

Here is a simple shell script to update npm packages in all subfolders:

#!/bin/bash

# Loop through each subfolder
for dir in */; do
    if [ -d "$dir" ]; then
        cd "$dir"
        if [ -f "package.json" ]; then
            echo "Updating npm packages in $dir"
            npm update
        fi
        cd ..
    fi
done

Explanation#

  • for dir in */; do: This loop iterates over all subfolders in the current directory.
  • if [ -d "$dir" ]; then: This checks if the item is a directory.
  • cd "$dir": This changes the current directory to the subfolder.
  • if [ -f "package.json" ]; then: This checks if the package.json file exists in the subfolder.
  • npm update: This updates all npm packages in the subfolder.
  • cd ..: This changes the directory back to the parent folder.

Running the Script#

  1. Save the script to a file, for example, update-npm-packages.sh.
  2. Make the script executable:
chmod +x update-npm-packages.sh
  1. Run the script:
./update-npm-packages.sh

Conclusion#

By using this shell script, you can easily update all npm packages in multiple projects located in subfolders. This can save time and ensure that all your projects are using the latest versions of their dependencies.


How to update all npm packages in multiple projects that sit in subfolders
https://zxce3.net/posts/cli/how-to-update-all-npm-packages-in-multiple-projects-that-sit-in-subfolders/
Author
Memet Zx
Published at
2023-07-25