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

最简单的方法是什么?

我知道的最好的方法是运行npm info express版本,然后手动更新package.json中的每个依赖项。一定有更好的办法。

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

有关纱线特定的解决方案,请参阅本堆栈溢出线程。


当前回答

npm check updates是一个实用程序,它使用所有依赖项的最新版本

看见https://www.npmjs.org/package/npm-check-updates

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

[编辑]如果你有一个现代版本的npm,一种稍微不那么侵入性(避免全局安装)的方法是:

$ npx npm-check-updates -u
$ npm install 

其他回答

如果您碰巧使用Visual Studio代码作为IDE,那么这是一个有趣的小扩展,可以使package.json的更新成为一个一键过程。

注意:更新package.json文件中的包后,运行npm更新以安装新版本。

版本镜头

GitLab报告

要查看哪些软件包具有可用的更新版本,请使用以下命令:

npm outdated

要只更新一个依赖项,只需使用以下命令:

npm install yourPackage@latest

例如:

我的package.json文件具有依赖关系:

"@progress/kendo-angular-dateinputs": "^1.3.1",

那么我应该写:

npm install @progress/kendo-angular-dateinputs@latest

save dev是什么意思?

npm install @progress/kendo-angular-dateinputs@latest --save-dev

正如npm安装文档所说:

-D、 --save dev:Package将显示在devDependencies中。

如果您使用的是yarn,yarn升级交互是一个非常时髦的工具,可以让您查看过时的依赖项,然后选择要更新的依赖项。

更多理由使用纱线代替npm。呵呵。

使用*作为最新版本的版本,包括不稳定版本使用最新版本作为最新稳定版本的版本定义使用LatestStablePackages使用最新的稳定版本号修改package.json

下面是一个示例:

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

我使用npm检查来实现这一点。

npm i -g npm npm-check
npm-check -ug #to update globals
npm-check -u #to update locals

另一个有用的命令列表,它将在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

更新:您可以使用yarn升级交互式--如果您使用yarn,则为最新版本