我从另一个项目中复制了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检查来实现这一点。

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,则为最新版本

其他回答

我通过查看https://github.com/tjunnone/npm-check-updates

$ npm install -g npm-check-updates
$ ncu
$ ncu -u # to update all the dependencies to latest
$ ncu -u "specific module name"  #in case you want to update specific dependencies to latest

备选方案是

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

每次使用npm更新时,它都会自动更新到最新版本。有关更多版本语法,请查看此处:https://www.npmjs.org/doc/misc/semver.html

由于距离最初的问题已经过去了近10年,许多答案要么过时,要么不推荐。

我会使用与包管理器无关的东西,即可以与npm、pnpm、yarn或其他东西一起工作。

最近我一直在使用taze

您可以将其添加到开发依赖项中并从那里运行,也可以在不安装npx-taze或pnx-taze等的情况下运行。

如果您正在使用Github,请使用Greenkeeper。https://greenkeeper.io/

这是一个Github集成,非常容易设置。安装后,它会自动在您指定的存储库中创建拉取请求(如果需要,也可以全部创建),并使您的代码始终保持最新,而无需强制您手动执行任何操作。然后,PR应该在CI服务上触发构建,根据成功或失败的检查,您可以继续找出触发问题的原因,或者CI通过时,只需合并PR。

在底部,您可以看到第一个构建最初失败,在提交(“升级到节点v6.9”)后测试通过,因此我可以最终合并PR。还附带了很多表情符号。

另一种选择是https://dependencyci.com/然而,我没有集中测试它。乍一看,Greenkeeper在一般IMO中看起来更好,并具有更好的整合性。

使用*作为最新版本的版本,包括不稳定版本使用最新版本作为最新稳定版本的版本定义使用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"
},