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

Try following command if you using npm 5 and node 8

npm update --save

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

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.

查看更多
不再属于我。
4楼-- · 2018-12-31 16:32

Here is a basic regex to match semantic version numbers so you can quickly replace them all with an asterisk.

Semantic Version Regex

([>|<|=|~|^|\s])*?(\d+\.)?(\d+\.)?(\*|\d+)

How to use

Select the package versions you want to replace in the JSON file.

screenshot:select the text you want to replace

Input the regex above and verify it's matching the correct text.

screenshot:input the semver regex above

Replace all matches with an asterisk.

screenshot:replace package versions with an asterisk

Run npm update --save

查看更多
看淡一切
5楼-- · 2018-12-31 16:32

The above commands are unsafe because you might break your module when switching versions. Instead I recommend the following

  • Set actual current node modules version into package.json using npm shrinkwrap command.
  • Update each dependency to the latest version IF IT DOES NOT BREAK YOUR TESTS using https://github.com/bahmutov/next-update command line tool
npm install -g next-update
// from your package
next-update
查看更多
深知你不懂我心
6楼-- · 2018-12-31 16:32
  • npm outdated
  • npm update

Should get you the latest wanted versions compatible for your app. But not the latest versions.

查看更多
骚的不知所云
7楼-- · 2018-12-31 16:33

This is what I did to update all the dependencies in package.json to latest:

npm install -g npm-check-updates
ncu -u --packageFile package.json 
查看更多
登录 后发表回答