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

其他回答

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 

备选方案是

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

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

如果使用npm 5和节点8,请尝试以下命令

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和package.json实现gruntfile.js魔法的项目。以下bash命令(多行命令)对我来说效果很好:

npm outdated --json --depth=0 | \
jq --ascii-output --monochrome-output '. | keys | .[]' | \
xargs npm install $1 --save-dev

这里的想法是:将npm过时的输出作为json传输到jq(jq是一个json命令行解析器/查询工具)(注意npm过时时使用--depth参数)jq将仅将输出剥离到顶级包名称。最后xargs将每个LIBRARYNAME一次一个放入npm install LIBRARYNAME--save dev命令

以上是我在机器运行时所做的工作:节点=v0.11.10 osx=10.9.2 npm=1.3.24

这需要:xargs公司http://en.wikipedia.org/wiki/Xargs(我相信是我的机器固有的)和jq公司http://stedolan.github.io/jq/(我用brew install jq安装了它)

注意:我只使用--save-dev将更新的库保存到json键devDependencies中的package.json,这是我的项目的要求,很可能不是你的。

之后,我用一个简单的

npm outdated --depth=0

此外,您可以使用

npm list --depth=0