Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
618 views
in Technique[技术] by (71.8m points)

node.js - 如何将package.json中的每个依赖关系更新为最新版本?(How do I update each dependency in package.json to the latest version?)

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.

(我从另一个项目复制了package.json,现在想将所有依赖项都升级到最新版本,因为这是一个新项目,如果出现问题,我不介意进行修复。)

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.

(我现在所知道的最好方法是运行npm info express version然后为每个npm info express version手动更新package.json。)

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"
  }
}

UPDATE 5/1/19 : Six years later and I am still maintaining npm-check-updates as a comprehensive solution to this problem.

(19年5月1日更新 :六年后,我仍在维护npm-check-updates作为该问题的综合解决方案。)

Enjoy!

(请享用!)

  ask by Raine Revere translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Looks like npm-check-updates is the only way to make this happen now.

(看起来npm-check-updates是现在做到这一点的唯一方法。)

npm i -g npm-check-updates
ncu -u
npm install

On npm <3.11:

(在npm <3.11上:)

Simply change every dependency's version to * , then run npm update --save .

(只需将每个依赖项的版本更改为* ,然后运行npm update --save 。)

( Note: broken in recent (3.11) versions of npm ).

(( 注意: 在最新(3.11)版本的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 .

(要查看哪些模块已过时,只需运行npm outdated 。)

It will list any installed dependencies that have newer versions available.

(它将列出具有较新版本的所有已安装依赖项。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...