我从另一个项目中复制了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脚本可以自动更新:

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

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 

在@kozlovd答案上展开,我构建了一个bash脚本,通过两个步骤更新任何npm脚本:

如果您收到错误,请手动计算npm包的数量。npm列表|wc-l这里用包的数量替换NUM_PKGS,如果在上一个命令中出现“UNMET DEPENDENCY”错误,则将$2替换为$4。

NUM_PKGS=9999; npm list --no-unicode | awk -v NUM_PKGS=$NUM_PKGS '{\
    if (NR>1 && NR <NUM_PKGS) {\
        pver=A[split($2,A,"@")];\
        print substr($2,0,length($2)-length(pver))"latest";\
    }\
}' | xargs -r npm i

说明:命令Nº2首先获取包名,并仅在出现“UNMET DEPENDENCY”错误的情况下对其行进行操作,然后awk迭代每个包名,它获取版本值并用“最新”替换,最后所有替换了版本的包都由xargs收集,xargs在“npm i”之后将它们连接起来,最终用最新版本安装所有包。此步骤可以使用没有设置版本的包或现有项目更新新项目

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

$npm更新

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

$npm审计修复--强制

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

$npm安装

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

我通过查看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