Update all npm packages in multiple projects in subfolders

By

Published · Updated

Tagged: CLI

Review and update npm dependencies across projects in subfolders with npm-check-updates, without deleting every node_modules directory first.

First run a read-only pass so you can see which projects would change:

#!/usr/bin/env bash

for dir in */; do
  if [ -f "${dir}package.json" ]; then
    echo "Checking $dir"
    (
      cd "$dir" || exit
      npx --yes npm-check-updates
    )
  fi
done

When you are ready to update a project, run this inside that project:

npx npm-check-updates -u
npm install
npm test

npm-check-updates -u changes the version ranges in package.json. npm install then updates the lockfile and installed dependencies.

Do not delete every node_modules directory as part of the loop. It adds work and makes it harder to test projects one at a time. Major dependency updates can contain breaking changes, so use a clean Git working tree, review each diff, and run that project’s own tests and build before moving to the next folder.

Want me to talk about your product? You can sponsor this site.

Related posts about cli: