Ilja
Ilja

Reputation: 46479

How can I update all npm packages/modules at once?

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.

Upvotes: 18

Views: 18815

Answers (7)

As of npm v8, you can pass --save to npm update, which will update the versions of everything in package.json.

After using npm outdated -p and cut for so long, I was surprised when someone pointed this out to me.

It just updates the minor versions, but that's usually what I want — if I try to update all the major versions, the chances that everything is compatible with everything is usually pretty low, so I have to do that fairly piecemeal.

Upvotes: 0

Matt
Matt

Reputation: 35251

One simple step:

$ npx npm-check-updates -u && npm i

This will set all of your packages in package.json to the latest version and apply the updates.

Upvotes: 25

Wesley Chan
Wesley Chan

Reputation: 41

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

Upvotes: 3

Mike
Mike

Reputation: 14586

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?

Upvotes: 1

eush77
eush77

Reputation: 4088

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.

Upvotes: 3

Zany Cadence
Zany Cadence

Reputation: 244

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.

Upvotes: 11

ajbisberg
ajbisberg

Reputation: 36

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.

Upvotes: 2

Related Questions