DEV Community

Takuya Matsuyama
Takuya Matsuyama

Posted on

One-liner to update npm dependency packages to the latest version using jq

If you have jq, a lightweight and flexible command-line JSON processor, installed in your computer, it's easy to manipulate the command to update dependencies to the latest version at once.

Update dependencies

Check:

cat package.json | jq -r '.dependencies | keys | join("\\n")'
Enter fullscreen mode Exit fullscreen mode

Perform installing:

cat package.json | jq -r '.dependencies | keys | map(. + "@latest") | join(" ")' | xargs npm i
Enter fullscreen mode Exit fullscreen mode

Update devDependencies

cat package.json | jq -r '.devDependencies | keys | map(. + "@latest") | join(" ")' | xargs npm i
Enter fullscreen mode Exit fullscreen mode

Follow me online

Top comments (2)

Collapse
 
richardeschloss profile image
Richard Schloss • Edited

How about npm update and npm update --dev? (ref)

Collapse
 
craftzdog profile image
Takuya Matsuyama

Because it does not always update packages to the latest versions if those are caret/tilde dependencies:

docs.npmjs.com/cli-commands/update...

It means that npm update tries to update while keep satisfying app's package dependencies.