Update all npm packages in multiple projects in subfolders

By

Published · Updated

Tagged: CLI

Check and update npm dependencies in every project inside a folder, with a bash loop or npm-check-updates --deep, and stop when a project's tests fail.

Say you have a folder with many projects, each with its own package.json. Here’s how to check and update the dependencies of all of them at once.

I use npm-check-updates (ncu), run with npx so there’s nothing to install globally. Version 23 needs Node.js 22.22.2 or later (or 24.15+, or 26+).

See what’s outdated, without changing anything

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
    (
      cd "$dir" || exit
      npx --yes npm-check-updates
    )
  fi
done

*/ matches only the folders directly inside the current one. Folders without a package.json are skipped. The ( ... ) runs each cd in a subshell, so the loop always comes back to the parent folder.

For each project you get a list like this, grouped by the kind of update:

Checking /Users/flavio/dev/api/package.json

Minor   Backwards-compatible features
 lodash  4.17.20  →  4.18.1
 ms        2.0.0  →   2.1.3

Run npx npm-check-updates -u to upgrade package.json

Without -u, ncu only prints. It doesn’t touch any file.

Or use --deep instead of a loop

ncu can find the projects by itself:

npx npm-check-updates --deep

--deep looks for every package.json under the current folder, at any depth. In my test it skipped the package.json files inside node_modules, and it also found a project nested two levels down, which the */ loop misses.

npx npm-check-updates --deep -u updates all those package.json files at once, but it doesn’t install anything. You still need to run npm install in each project, so for updating I prefer the loop below.

If your folder is an npm workspaces monorepo (one root package.json with a workspaces field), use --workspaces instead of --deep.

Update each project and test it

When you’re ready, run this loop. It updates one project at a time, installs, runs the tests, and stops at the first project that fails:

#!/usr/bin/env bash

for dir in */; do
  [ -f "${dir}package.json" ] || continue
  echo "== Updating $dir"
  (
    cd "$dir" || exit 1
    npx --yes npm-check-updates -u --target minor &&
      npm install &&
      npm test --if-present
  ) || { echo "Stopped at $dir"; exit 1; }
done

What each step does:

When a project fails, the loop prints Stopped at api/ and exits, so you can fix that project before moving on. When you run the loop again, the projects that were already updated get no new changes to package.json.

One gotcha: npm init -y creates a placeholder test script, echo "Error: no test specified" && exit 1. --if-present doesn’t help there, because the script exists. It always fails, so remove it or replace it with a real test.

Pick how far to update

By default ncu goes to the latest version, majors included, and major versions can contain breaking changes. These options keep the update smaller:

# only patch releases (4.17.20 → 4.17.23)
npx npm-check-updates -u --target patch

# patch and minor releases, no majors
npx npm-check-updates -u --target minor

# skip some packages
npx npm-check-updates -u --reject react,react-dom

# only some packages
npx npm-check-updates -u --filter "eslint*"

# choose each update from a list
npx npm-check-updates -i

# ignore versions published in the last 7 days
npx npm-check-updates -u --cooldown 7d

--cooldown only considers versions older than the period you give it. It’s a simple guard against a freshly published version that turns out to be broken or compromised.

To learn what counts as a patch, minor or major update, and how ^ and ~ ranges work, see semantic versioning using npm.

npm update vs npm-check-updates

npm update is the other option, and it works differently. It installs the newest version allowed by the range already in package.json, and updates package-lock.json. It doesn’t change package.json.

I tested it with npm 10: with "lodash": "^4.17.20", npm update installed 4.18.1 and left package.json as it was. It never crosses a major version, and it does nothing for exact versions like "4.17.20".

ncu -u rewrites the ranges themselves, so it can move you to a new major, or bump exact versions. I explain both in more detail in update all the Node dependencies to their latest version.

Before you run it

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

Related posts about cli: