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
and then update each dependency in package.json
manually. There must be a better way.
{
"name": "myproject",
"description": "my node project",
"version": "1.0.0",
"dependencies": {
"express": "^3.0.3", // how do I get these bumped to latest?
"mongodb": "^1.2.5",
"underscore": "^1.4.2"
}
}
For Yarn-specific solutions, refer to this Stack Overflow question.
npm outdated | sed '1d; s/ .*/@latest/' | xargs npm i --save
- anyone It 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 Stack Overflow answer.
Answered 2023-09-20 20:26:14
*
in package.json since you might end up automatically installing a new module version with breaking changes that break your app. Since we're using --save
here, the *
is replaced with each package's current version. - anyone npm install --save
the wildcard is left in my package.json
. - anyone update
doesn't work either, for me. I'm still left with the wildcards. Is there any documentation about this that you know of, or any other resources I might look at? - anyone npm install -g npm-check-updates
to install, then npm-check-updates
to check if your dependencies have updates, and npm-check-updates -u
to update your package.json versions. Then it's just npm install
and it will download new versions. - anyone npm update --save
instead of npm update --save-dev
. - anyone 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
Answered 2023-09-20 20:26:14
npx npm-check-updates -u
was the answer for me - anyone
npm outdated
npm update
git commit package-lock.json
npm install -g npm-check-updates
npm-check-updates
npm shrinkwrap
git commit package-lock.json
Be sure to shrinkwrap your deps, or you may wind up with a dead project. I pulled out a project the other day and it wouldn't run because my deps were all out of date/updated/a mess. If I'd shrinkwrapped, npm would have installed exactly what I needed.
For the curious who make it this far, here is what I recommend:
npm-check-updates
or npm outdated
to suggest the latest versions.# `outdated` is part of newer npm versions (2+)
$ npm outdated
# If you agree, update.
$ npm update
# OR
# Install and use the `npm-check-updates` package.
$ npm install -g npm-check-updates
# Then check your project
$ npm-check-updates
# If you agree, update package.json.
$ npm-check-updates -u
$ rm -rf node_modules
$ npm install
npm-shrinkwrap.json
with npm shrinkwrap
$ rm npm-shrinkwrap.json
$ npm shrinkwrap
npm install
will now use exact versions in npm-shrinkwrap.json
If you check npm-shrinkwrap.json
into git, all installs will use the exact same versions.
This is a way to transition out of development (all updates, all the time) to production (nobody touch nothing).
Answered 2023-09-20 20:26:14
npm-shrinkwrap.json
into source, and commit whenever you update, you can always 'go back to where you were'. I overlooked shrinkwrap feature when I started. - anyone npm update
only updates to the semver version, not the latest. - anyone To update one dependency to its lastest version without having to manually open the package.json
and change it, you can run
npm install {package-name}@* {save flags?}
i.e.
npm install express@* --save
This flow is compatible with workspaces, i.e.
npm --workspace some/package install express@*
For reference, npm-install
Note: Some npm versions may need latest flag instead, i.e. npm install express@latest
As noted by user Vespakoen on a rejected edit, it's also possible to update multiple packages at once this way:
npm install --save package-nave@* other-package@* whatever-thing@*
He also apports a one-liner for the shell based on npm outdated
. See the edit for code and explanation.
PS: I also hate having to manually edit package.json
for things like that ;)
Answered 2023-09-20 20:26:14
npm outdated -l
to show whether each package is a dependency or devDependency. Use npm install --save-dev
to save as a devDependency. - anyone ncu express mocha chai
. You can also exclude packages with ncu -x mocha
. I agree the above is the simplest solution though for updating a single package. - anyone npm install {package-name}@latest {save flags}
- anyone If you happen to be using Visual Studio Code as your IDE, this is a fun little extension to make updating package.json
a one click process.
note: After updating packages in package.json
file, run npm update
to install the new versions.
Answered 2023-09-20 20:26:14
This works as of npm 1.3.15.
"dependencies": {
"foo": "latest"
}
Answered 2023-09-20 20:26:14
npm shrinkwrap
to freeze deps. - anyone "react": "16.9.0"
and then I added latest to it and ran npm i
, after this how do I find which version of react is now in my project? since "react":"latest"
is whats left in my package.json, not a number even after I did npm i
- anyone *
as the version for the latest releases, including unstablelatest
as version definition for the latest stable versionLatestStablePackages
Here is an example:
"dependencies": {
"express": "latest" // using the latest STABLE version
, "node-gyp": "latest"
, "jade": "latest"
, "mongoose": "*" // using the newest version, may involve the unstable releases
, "cookie-parser": "latest"
, "express-session": "latest"
, "body-parser": "latest"
, "nodemailer":"latest"
, "validator": "latest"
, "bcrypt": "latest"
, "formidable": "latest"
, "path": "latest"
, "fs-extra": "latest"
, "moment": "latest"
, "express-device": "latest"
},
Answered 2023-09-20 20:26:14
npm run build
. - anyone npm shrinkwrap
is the way to go to freeze version numbers for a production release. But to convert "latest" into exact semvar numbers one could use something like "scripts": { "build" : "npx --yes npm-check-updates --upgrade" }
in package.json. - anyone To see which packages have newer versions available, then use the following command:
npm outdated
to update just one dependency just use the following command:
npm install yourPackage@latest
For example:
My package.json
file has dependency:
"@progress/kendo-angular-dateinputs": "^1.3.1",
then I should write:
npm install @progress/kendo-angular-dateinputs@latest
What does --save-dev
mean?
npm install @progress/kendo-angular-dateinputs@latest --save-dev
-D, --save-dev: Package will appear in your devDependencies.
Answered 2023-09-20 20:26:14
I really like how npm-upgrade works. It is a simple command line utility that goes through all of your dependencies and lets you see the current version compared to the latest version and update if you want.
Here is a screenshot of what happens after running npm-upgrade
in the root of your project (next to the package.json
file):
For each dependency you can choose to upgrade, ignore, view the changelog, or finish the process. It has worked great for me so far.
To be clear this is a third party package that needs to be installed before the command will work. It does not come with npm itself:
npm install -g npm-upgrade
Then from the root of a project that has a package.json file:
npm-upgrade
Answered 2023-09-20 20:26:14
npm-upgrade
did not work for me, but npm upgrade
did and it updated my package.json file which was exactly what I was looking for. - anyone npm-upgrade
is that you get to see exactly what is being upgraded and pick and choose which ones get upgraded. npm upgrade
may work fine for most people but sometimes you need to have a little more control when upgrading. - anyone upgrade
is an alias for update
, so there should be no difference between them. However, npm 6.2.0 is buggy, and may not udpate package.json
. - anyone npx npm-upgrade
- quite cool! :) - anyone The only caveat I have found with the best answer above is that it updates the modules to the latest version. This means it could update to an unstable alpha build.
I would use that npm-check-updates utility. My group used this tool and it worked effectively by installing the stable updates.
As Etienne stated above: install and run with this:
$ npm install -g npm-check-updates
$ npm-check-updates -u
$ npm install
Answered 2023-09-20 20:26:14
rm -rf node_modules
before npm install
got rid of some dependency warnings for me. - anyone I use npm-check
to achieve this.
npm i -g npm npm-check
npm-check -ug # To update globals
npm-check -u # To update locals
Another useful command list which will keep exact version numbers in package.json:
npm cache clean
rm -rf node_modules/
npm i -g npm npm-check-updates
ncu -g # Update globals
ncu -u # Update locals
npm I
You can use yarn upgrade-interactive --latest
if you are using Yarn.
Answered 2023-09-20 20:26:14
yarn upgrade-interactive --latest
is a better option now - anyone Safe update
Use npm outdated
to discover dependencies that are out of date.
Use npm update
to perform safe dependency upgrades.
Use npm install <packagename>@latest
to upgrade to the latest major version of a package.
Breaking Update
Use npx npm-check-updates -u
.
npm install
to upgrade all dependencies to their latest major versions.
Answered 2023-09-20 20:26:14
npm install @latest
should really be npm install <PACKAGE>@latest
. - anyone Here is a basic regex to match semantic version numbers so you can quickly replace them all with an asterisk.
([>|<|=|~|^|\s])*?(\d+\.)?(\d+\.)?(\*|\d+)
Select the package versions you want to replace in the JSON file.
Input the regex above and verify it's matching the correct text.
Replace all matches with an asterisk.
Run npm update --save
Answered 2023-09-20 20:26:14
('|")([>|<|=|~|^|\s])*?(\d+\.)?(\d+\.)?(\*|\d+)
- anyone :
and press ctrl+d
multiple times until you select them all, then go to the version number (press right arrow 2 times) and press ctrl space, then write "*"
- anyone If you want to use a gentle approach via a beautiful (for terminal) interactive reporting interface I would suggest using npm-check.
It's less of a hammer and gives you more consequential knowledge of, and control over, your dependency updates.
To give you a taste of what awaits here's a screenshot (scraped from the git page for npm-check):
Answered 2023-09-20 20:26:14
As of npm version 5.2.0, there is a way to run this in a single line without installing any additional packages to your global npm registry nor locally to your application. This can be done by leveraging the new npx
utility that's bundled with npm. (Click here to learn more.)
Run the following command in the root of your project:
npx npm-check-updates -u && npm i
Answered 2023-09-20 20:26:14
This feature has been introduced in npm v5. Update to npm using npm install -g npm@latest
and to update package.json:
delete folder node_modules and package-lock.json (if you have any)
run npm update
. This will update the dependencies in package.json to the latest, based on semantic versioning.
To update to the very latest version, you can go with npm-check-updates
.
Answered 2023-09-20 20:26:14
I recently had to update several projects that were using npm and package.json for their gruntfile.js magic. The following bash command (multiline command) worked well for me:
npm outdated --json --depth=0 | \
jq --ascii-output --monochrome-output '. | keys | .[]' | \
xargs npm install $1 --save-dev
The idea here:
To pipe the npm outdated
output as json, to jq
(jq is a json command line parser/query tool)
(notice the use of --depth
argument for npm outdated
)
jq will strip the output down to just the top level package name only.
finally xargs puts each LIBRARYNAME one at a time into a npm install LIBRARYNAME --save-dev
command
The above is what worked for me on a machine runnning: node=v0.11.10 osx=10.9.2 npm=1.3.24
this required:
xargs http://en.wikipedia.org/wiki/Xargs (native to my machine I believe)
and
jq http://stedolan.github.io/jq/ (I installed it with brew install jq
)
Note: I only save the updated libraries to package.json inside of the json key devDependancies
by using --save-dev
, that was a requirement of my projects, quite possible not yours.
Afterward I check that everything is gravy with a simple
npm outdated --depth=0
Also, you can check the current toplevel installed library versions with
npm list --depth=0
Answered 2023-09-20 20:26:14
awk
instead: npm outdated --depth=0 | grep -v "^Package" | awk '{print $1}' | xargs npm install $1 --save-dev
- anyone cat package.json|jq -r '.devDependencies|keys|map(.+"@latest")|@sh'|xargs npm install --save-dev
- anyone If you use Yarn, the following command updates all packages to their latest version:
yarn upgrade --latest
From their documentation:
The
upgrade --latest
command upgrades packages the same as the upgrade command, but ignores the version range specified in package.json. Instead, the version specified by the latest tag will be used (potentially upgrading the packages across major versions).
Answered 2023-09-20 20:26:14
package.json
- github.com/yarnpkg/yarn/issues/4390 - anyone Use Updtr!
Based on npm outdated, updtr installs the latest version and runs npm test for each dependency. If the test succeeds, updtr saves the new version number to your package.json. If the test fails, however, updtr rolls back its changes.
Answered 2023-09-20 20:26:14
If you are using yarn
, yarn upgrade-interactive
is a really sleek tool that can allow you to view your outdated dependencies and then select which ones you want to update.
More reasons to use Yarn over npm
. Heh.
Answered 2023-09-20 20:26:14
package.json
- github.com/yarnpkg/yarn/issues/4390 - anyone Commands that I had to use to update package.json
for NPM 3.10.10
:
npm install -g npm-check-updates
ncu -a
npm install
Background:
I was using the latest command from @josh3736 but my package.json
was not updated. I then noticed the description text when running npm-check-updates -u
:
The following dependency is satisfied by its declared version range, but the installed version is behind. You can install the latest version without modifying your package file by using npm update. If you want to update the dependency in your package file anyway, run ncu -a.
Reading the documentation for npm-check-updates you can see the difference:
https://www.npmjs.com/package/npm-check-updates
-u, --upgrade: overwrite package file
-a, --upgradeAll: include even those dependencies whose latest version satisfies the declared semver dependency
ncu is an alias for npm-check-updates
as seen in the message when typing npm-check-updates -u
:
[INFO]: You can also use ncu as an alias
Answered 2023-09-20 20:26:14
-a
is the default behavior, and overwriting the package.json is left solely to the -u
option. - anyone If you don't want to install global npm-check-updates you can simply run that:
node -e "const pk = JSON.parse(require('fs').readFileSync('package.json', 'utf-8'));require('child_process').spawn('npm', ['install', ...Object.keys(Object.assign({},pk.dependencies, pk.devDependencies)).map(a=>a+'@latest')]).stdout.on('data', d=>console.log(d.toString()))"
Answered 2023-09-20 20:26:14
If you're looking for an easier solution that doesn't involve installing npm packages, I'd checkout updatepackagejson.com
Answered 2023-09-20 20:26:14
As it's almost been 10 years since the original question, and many of the answers are either outdated or not recommended.
I would use something which is package manager-agnostic, i.e., can work with npm, pnpm, Yarn or others.
Lately I have been using taze.
You can either add it to your development dependencies and run from there or run without installation with npx taze
or pnpx taze
, etc.
Answered 2023-09-20 20:26:14
I solved this by using the instructions from npm-check-updates:
npm install -g npm-check-updates
ncu
ncu -u # To update all the dependencies to the latest
ncu -u "specific module name" # In case you want to update specific dependencies to the latest
Answered 2023-09-20 20:26:14
The above commands are unsafe because you might break your module when switching versions. Instead I recommend the following
npm shrinkwrap
command.npm install -g next-update // from your package next-update
Answered 2023-09-20 20:26:14
I found another solution for recent versions of NPM. I want to replace all the "*" dependencies with the explicit latest version number. None of the methods discussed has worked for me.
I did:
npm-check-updates -u
Everything in package.json now is updated to the last version.
Answered 2023-09-20 20:26:14
Try the following command if you are using npm 5 and Node.js 8:
npm update --save
Answered 2023-09-20 20:26:14
update
command does not seem to bump dependencies beyond the original definition. If package.json
declares "1.2.3"
exactly you won't get 1.2.4
. That can be good or bad :) - anyone The following code (which was accepted) wrote me something like "it takes too long blah-blah" and did nothing. Probably using the global flag was the problem, idk.
npm i -g npm-check-updates
ncu -u
npm install
I decided to use my text editor and follow a semi-manual approach instead.
I copied a list like this (just a lot longer) from the dev dependencies of my package.json
to the notepad++ text editor:
"browserify": "10.2.6",
"expect.js": "^0.3.1",
"karma": "^0.13.22",
"karma-browserify": "^5.2.0",
I set the search mode to regular expression, used the ^\s*"([^"]+)".*$
pattern to get the package name and replaced it with npm uninstall \1 --save-dev \nnpm install \1 --save-dev
. Clicked on "replace all". The otput was this:
npm uninstall browserify --save-dev
npm install browserify --save-dev
npm uninstall expect.js --save-dev
npm install expect.js --save-dev
npm uninstall karma --save-dev
npm install karma --save-dev
npm uninstall karma-browserify --save-dev
npm install karma-browserify --save-dev
I copied it back to bash and hit enter. Everything was upgraded and working fine. That's all.
"browserify": "^16.1.0",
"expect.js": "^0.3.1",
"karma": "^2.0.0",
"karma-browserify": "^5.2.0",
I don't think it is a big deal, since you have to do it only every now and then, but you can easily write a script, which parses the package.json
and upgrades your packages. I think it is better this way, because you can edit your list if you need something special, for example keeping the current version of a lib.
Answered 2023-09-20 20:26:14
--packageFile package.json
so that knows not to wait for stdin. - anyone It's wild to me that 90% of answers is some variant of "use npm-check-updates
". Here's what I do (relevant code):
{
"devDependencies": {
"updates": "^13.0.5" // the version here could be "latest" or "*" tbh...
},
"scripts": {
"test:dependencies": "updates --update ./",
}
}
Running npm run test:dependencies
(or whatever your dependency update script is called) will check your package.json
for the latest versions of every package listed, and it'll let you know when the latest version was published. Run npm i
after that and you'll be up to date!
Also, unlike npm-check-updates
, updates
has zero dependencies (ncu has 29, at the time of this post).
Answered 2023-09-20 20:26:14