Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update each dependency in package.json to the latest version?

Tags:

node.js

npm

I copied package.json from another project and now want to bump all of the dependencies to their latest versions since this is a fresh project and I don't mind fixing something if it breaks.

What's the easiest way to do this?

The best way I know is to run npm info express version then update each dependency in package.json manually. There must be a better way.

{   "name": "myproject",   "description": "my node project",   "version": "1.0.0",   "engines": {     "node": "0.8.4",     "npm": "1.1.65"   },   "private": true,   "dependencies": {     "express": "~3.0.3", // how do I get these bumped to latest?     "mongodb": "~1.2.5",     "underscore": "~1.4.2",     "rjs": "~2.9.0",     "jade": "~0.27.2",     "async": "~0.1.22"   } } 

For Yarn specific solutions refer to this Stack Overflow thread.

like image 319
Raine Revere Avatar asked Apr 18 '13 02:04

Raine Revere


People also ask

How do you update a specific package in package json?

To update a specific package, we need to run the npm update command followed by the package name. Sometimes, you want to update a package to the specific version in such cases you need to use npm install command by specifying a version number after the package name.

How can you update a particular project dependency in npm?

The best way I know is to run npm info express version then update each dependency in package. json manually.


Video Answer


2 Answers

Looks like npm-check-updates is the only way to make this happen now.

npm i -g npm-check-updates ncu -u npm install 

On npm <3.11:

Simply change every dependency's version to *, then run npm update --save. (Note: broken in recent (3.11) versions of npm).

Before:

  "dependencies": {     "express": "*",     "mongodb": "*",     "underscore": "*",     "rjs": "*",     "jade": "*",     "async": "*"   } 

After:

  "dependencies": {     "express": "~3.2.0",     "mongodb": "~1.2.14",     "underscore": "~1.4.4",     "rjs": "~2.10.0",     "jade": "~0.29.0",     "async": "~0.2.7"   } 

Of course, this is the blunt hammer of updating dependencies. It's fine if—as you said—the project is empty and nothing can break.

On the other hand, if you're working in a more mature project, you probably want to verify that there are no breaking changes in your dependencies before upgrading.

To see which modules are outdated, just run npm outdated. It will list any installed dependencies that have newer versions available.

For Yarn specific solution, refer to this StackOverflow answer.

like image 85
josh3736 Avatar answered Oct 14 '22 20:10

josh3736


npm-check-updates is a utility that automatically adjusts a package.json with the latest version of all dependencies

see https://www.npmjs.org/package/npm-check-updates

$ npm install -g npm-check-updates $ ncu -u $ npm install  

[EDIT] A slightly less intrusive (avoids a global install) way of doing this if you have a modern version of npm is:

$ npx npm-check-updates -u $ npm install  
like image 39
Etienne Avatar answered Oct 14 '22 22:10

Etienne