How to update version of a package in package-lock.json and/or package.json using npm to latest version?

Viewed 3466

Say you get a warning in some libraries in a repo about security concerns from github. You want to quickly bump the version just to make the github warnings going away. You are not worried about re-installing, rebuilding and testing.

Is there a way to do this with npm?

npm update mypackage does not do anything.

2 Answers

Now it works different, if you notice package versions in package lock.json have a prefix, sometimes its ~ sometimes ^, they have big importance when it comes to package updating, as fixing package mismatches is the worst hell.

Suppose you have package in package.json called packX with version ~1.1.1 or ^1.1.1

When you run npm update for packX npm will first of all check the version prefix for it.

If there is ~ in this case it will be understood as install packX version >=1.1.1 and <1.2.0 so the highest version it can install can only be in range of 1.1.N, it will not go up to 1.2.N.

If there is ^ then it will be understood as >=1.1.1 <2.0.0 so the highest version that can be installed will be in range of 1.N.N but connot go up to 2.N.N

Hope My explication is clear enough, anyways you can check the docs for details

npm update will only update minor versions.

Eg: It will update version 1.2.3 to 1.5.2
But it will not update version 1.2.3 to 2.0.1 because there can be breaking changes.

To check new major releases of the packages, you run npm outdated

To update to a new major versions for all the packages, you can use npm-check-updates

npm install -g npm-check-updates
Then run ncu -u

This will upgrade all the versions in the package.json file, to dependencies and devDependencies, so npm can install the new major version. Now you can update packages to new major releases by npm update

Reference

Related