Skip to content

/commands/npm/npm-update-packages-to-latest

npm update all packages to the latest version (npm outdated, npm update)

verified onenvironmentlocal · windows 10.0.26200 arm64 · GNU bash 5.3.15 (Git Bash) · 11.19.0updated
tool
npm
versions
npm 7+
os
any

To update packages, first run npm outdated to see what is behind, then npm update to move every dependency to the newest version allowed by the range in package.json (for ^4.17.20, the newest 4.x). To jump to a new major version, which npm update never does, run npm install pkg@latest for that package.

See what is outdated, then update

bash
npm init -y >/dev/null
npm install --no-audit --no-fund --silent lodash@4.17.20 is-odd@2.0.0
npm outdated
echo "npm outdated exit code: $?"
npm update --no-audit --no-fund --silent
echo "--- after npm update"
npm ls

Output

Package  Current  Wanted  Latest  Location             Depended by
is-odd     2.0.0   2.0.0   3.0.1  node_modules/is-odd  work
lodash   4.17.20  4.18.1  4.18.1  node_modules/lodash  work
npm outdated exit code: 1
--- after npm update
work@1.0.0 /tmp/work
├── is-odd@2.0.0
└── lodash@4.18.1

npm outdated lists three versions for each package. Current is what is installed, Wanted is the newest version that satisfies the range in package.json, and Latest is the version tagged latest on the registry. It exits with code 1 when anything is outdated, which is handy as a CI check. npm update then moved lodash to the Wanted version and left is-odd alone, because 3.0.1 is outside ^2.0.0.

Move to a new major version

bash
npm init -y >/dev/null
npm install --no-audit --no-fund --silent is-odd@2.0.0
npm install --no-audit --no-fund --silent is-odd@latest
npm pkg get dependencies
npm outdated && echo "nothing outdated"

Output

{
  "is-odd": "^3.0.1"
}
nothing outdated

npm install pkg@latest installs the latest release and rewrites the range in package.json, so it is the command for major upgrades. Do them one package at a time and run your tests in between.

Commands for updating

CommandWhat it does
npm outdatedShow Current, Wanted and Latest for direct dependencies
npm outdated --allInclude transitive dependencies
npm updateUpdate everything to the Wanted version (within package.json ranges)
npm update lodashUpdate one package within its range
npm update --saveAlso raise the ranges in package.json to the installed versions
npm install lodash@latestInstall the latest version, even a new major, and save it
npm update -gUpdate globally installed packages
npx npm-check-updates -uThird-party tool: rewrite every range to the latest version, then npm install

Pitfalls

  • Caret and tilde. ^1.2.3 allows minor and patch updates, ~1.2.3 only patches, and an exact 1.2.3 allows nothing: npm update cannot move a pinned dependency.
  • 0.x versions. For ^0.2.3 the caret allows only 0.2.x, because npm treats the minor number as breaking below 1.0.
  • Peer dependency conflicts. A major upgrade can make the tree unsolvable and fail with ERESOLVE unable to resolve dependency tree. Upgrade related packages together (for example react and react-dom).
  • Lockfile in CI. Updates change package-lock.json; commit it, and let CI install with npm ci so it gets exactly the tested versions.

How to confirm it worked

Run npm outdated again: no output and exit code 0 mean every package is at its Wanted version (and at Latest, if the ranges allow it). npm ls pkg shows the installed version, and git diff package-lock.json shows exactly what changed.

FAQ

Why did npm update not install the latest major version?

npm update respects the ranges in package.json. A dependency saved as ^2.0.0 never moves to 3.x with npm update; install the new major explicitly with npm install pkg@latest, after reading its release notes.

Does npm update change package.json?

In npm 7 and later it updates node_modules and package-lock.json, but leaves the ranges in package.json as they are unless you pass --save. npm install pkg@latest does rewrite the entry in package.json.

How do I update every dependency to its latest major version at once?

npm itself has no single command for that. The npm-check-updates package does it: npx npm-check-updates -u rewrites package.json to the latest versions, then run npm install. Review the list first, because major versions can break your code.

Sources