`npm update`, but actually useful

Intuitively, to update a package using npm, you might assume you need to run something like:

npm update

This command does nothing. The correct command is:

npm install <package name>@latest

As a human being who occasionally needs to update libraries in a software project, I expect my package manager to have some shortcut to do this. I want:

  • Determine the latest version for all libraries (or, if specified, a single specific library)
  • Said versions installed locally
  • The package file (package.json, requirements.txt, etc.) to be updated with the now-installed version
  • The package lockfile to be updated

Supposedly this just isn’t possible with npm, according to the first 4 answers in the top Stack Overflow question for this topic. The 5th answer gives us a hint to something that works for doing a package at a time, but needs to resort to xargs to update all packages at once.

npm outdated | cut -d" " -f1 | tail -n +2 | sed 's/$/@latest/' | xargs npm i