How do I update each dependency in package.json to

2018-12-31 15:56发布

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 of now is to run npm info express version then update package.json manually for each one. 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"
  }
}

I am now a collaborator on npm-check-updates, which is a great solution to this problem.

30条回答
与风俱净
2楼-- · 2018-12-31 16:42

Alternative is

"dependencies":{
    "foo" : ">=1.4.5"
}

everytime you use npm update , it automatically update to the latest version. For more version syntax, you may check here: https://www.npmjs.org/doc/misc/semver.html

查看更多
泪湿衣
3楼-- · 2018-12-31 16:42

Solution without additional packages

Change every dependency's version to *:

"dependencies": {
    "react": "*",
    "react-google-maps": "*"
  }

Then run npm update --save.

Some of your packages were updated, but some not?

"dependencies": {
    "react": "^15.0.1",
    "react-google-maps": "*"
  }

This is the tricky part, it means your local version of "react" was lower than the newest one. In this case npm downloaded and updated "react" package. However your local version of "react-google-maps" is the same as the newest one.

If you still want to "update" unchanged *, you have to delete these modules from node_modules folder.

e.g. delete node_modules/react-google-maps.

Finally run again npm update --save.

"dependencies": {
    "react": "^15.0.1",
    "react-google-maps": "^4.10.1"
  }

Do not forget to run npm update --save-dev if you want to update development dependencies.

查看更多
临风纵饮
4楼-- · 2018-12-31 16:44

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 
查看更多
泛滥B
5楼-- · 2018-12-31 16:44

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 --save

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 --save
查看更多
深知你不懂我心
6楼-- · 2018-12-31 16:44

Ncu is a new alias to check for updates. By doing so you do not have to manually update ur version numbers in package.json ncu does it for you . Follow the method below if you are on a Linux machine

sudo npm i -g npm-check-updates
// decide between -u or -a
ncu -u, --upgrade and overwrite package file
ncu -a, --upgradeAll include even those dependencies whose latest 
          version satisfies the declared server dependency
sudo npm install
查看更多
谁念西风独自凉
7楼-- · 2018-12-31 16:44

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):

enter image description here

查看更多
登录 后发表回答