11

I'm struggling to find a way to update all npm packages in one go, some articles suggest that package.json file should be edited where all version numbers need to be changed to * therefore forcing node to grab latest versions, but others state that such method is not considered good. Ideally, I want to find a command line option for this.

Ilja
  • 44,142
  • 92
  • 275
  • 498

6 Answers6

14

One simple step:

$ npm i -g npm-check-updates && ncu -u && npm i

This will install ncu, use it set all of your packages in package.json to the latest version and finally apply the updates.

binarynoise
  • 364
  • 3
  • 14
Matt
  • 33,328
  • 25
  • 83
  • 97
8

npm outdated is the command that you want to run to find all of the packages that are not up-to-date. You could pipe the output of npm output -json into a file and then iterate over the JSON to install the latest versions of the packages.

Zany Cadence
  • 219
  • 2
  • 6
2

You can try these one-liners.

Update all dependencies:

$ npm out --long --parseable |grep 'dependencies$' |cut -d: -f4 |xargs npm install --save

Update all devDependencies:

$ npm out --long --parseable |grep 'devDependencies$' |cut -d: -f4 |xargs npm install --save-dev

Keep in mind though that this is not usually a good idea as you might have to change something in the process of upgrading a package. If your project has many dependencies it is better to update them one by one or in small groups and run tests frequently.

eush77
  • 3,870
  • 1
  • 23
  • 30
1
npm outdated -p | cut -d ':' -f 5 | xargs npm install

Explaination

npm outdated -p: get a list of outdated packages and put them into parseable lines

Example line:

<project_path>\node_modules\<package_name>:<package_name>@<wanted_version>:<package_name>@<current_version>:<package_name>@<latest_version>:<module_name>

cut -d ':' -f 5: get the latest version
xargs npm install: install package by package

0

For a single module you could try npm install --save module@latest That would change package.json too. You could write a shell script or script in nodejs to iterate though package.json and update all of the modules.

ajbisberg
  • 16
  • 2
0

Recursive update of all modules can performed with npm update:

  • for locally installed modules: npm update --depth 9999 --dev
  • for globally installed modules: npm update --depth 9999 --dev -g

A ready-to-use NPM-script to update all Node.js modules with all its dependencies:
How to update all Node.js modules automatically?

Mike
  • 14,010
  • 29
  • 101
  • 161