我从另一个项目中复制了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"
  }
}

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


当前回答

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

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

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

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

其他回答

NPM脚本可以自动更新:

{
    "_cmd-update-modules": "npm run devops-update-modules",
    "scripts": {
        "create-global-node-modules-folder": "if not exist \"%appdata%\\npm\\node_modules\" mkdir %appdata%\\npm\\node_modules",
        "npm-i-g": "npm i npm@latest -g",
        "npm-check-i-g": "npm i npm-check@latest -g",
        "eslint-i-g": "npm i eslint@latest -g",
        "npm-check-u-l": "npm-check \"C:\\Program Files\\nodejs\\node_modules\\npm\" -y",
        "npm-check-u-g": "npm-check \"C:\\Program Files\\nodejs\\node_modules\\npm\" -y -g",
        "npm-deep-update-l": "npm update --depth 9999 --dev",
        "npm-deep-update-g": "npm update --depth 9999 --dev -g",
        "npm-cache-clear": "npm cache clear --force",
        "devops-update-modules": "npm run create-global-node-modules-folder && npm run npm-i-g && npm run npm-check-i-g && npm run eslint-i-g && npm run npm-check-u-l && npm run npm-check-u-g && npm run npm-deep-update-l && npm run npm-deep-update-g && npm run npm-cache-clear"
    }
}

有关详细信息和分步手册:https://stackoverflow.com/a/34295664

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

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

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

上述命令不安全,因为在切换版本时可能会损坏模块。相反,我建议如下

使用npm shrinkwrap命令将当前节点模块的实际版本设置为package.json。如果每个依赖项没有破坏您的测试,请使用https://github.com/bahmutov/next-update命令行工具

npm install -g next-update
// from your package
next-update

在2023年,我简单地使用了:

$npm更新

许多软件包都在此时更新,但您可能会收到一条消息,其中一些需要修复。在这种情况下,我跑了:

$npm审计修复--强制

最后,为了确保它已全部安装,我运行

$npm安装

就是这样,我的包更新了。无需手动编辑配置文件。