简体   繁体   English

如何一次更新所有npm软件包/模块?

[英]How can I update all npm packages/modules at once?

I'm struggling to find a way to update all npm packages in one go, some articles suggest that package.json file should be edited where all version numbers need to be changed to * therefore forcing node to grab latest versions, but others state that such method is not considered good. 我正在努力寻找一种一次性更新所有npm软件包的方法,有些文章建议应编辑package.json文件,其中所有版本号都需要更改为*从而迫使节点获取最新版本,但是其他人指出这种方法不被认为是好的。 Ideally, I want to find a command line option for this. 理想情况下,我想为此找到一个命令行选项。

npm outdated is the command that you want to run to find all of the packages that are not up-to-date. npm outdated是您要运行的命令,用于查找所有不是最新的软件包。 You could pipe the output of npm output -json into a file and then iterate over the JSON to install the latest versions of the packages. 您可以将npm output -json到文件中,然后遍历JSON以安装软件包的最新版本。

You can try these one-liners. 您可以尝试这些单线。

Update all dependencies: 更新所有依赖项:

$ npm out --long --parseable |grep 'dependencies$' |cut -d: -f4 |xargs npm install --save

Update all devDependencies: 更新所有devDependencies:

$ npm out --long --parseable |grep 'devDependencies$' |cut -d: -f4 |xargs npm install --save-dev

Keep in mind though that this is not usually a good idea as you might have to change something in the process of upgrading a package. 请记住,但这通常不是一个好主意,因为您可能需要在升级软件包的过程中进行一些更改。 If your project has many dependencies it is better to update them one by one or in small groups and run tests frequently. 如果您的项目有很多依赖关系,则最好一一或成组地更新它们并经常运行测试。

One simple step: 一个简单的步骤:

$ npm i -g npm-check-updates && ncu -a && npm i $ npm i -g npm-check-updates && ncu -a && npm i

This will set all of your packages in package.json to the latest version. 这会将package.json中的所有软件包都设置为最新版本。

For a single module you could try npm install --save module@latest That would change package.json too. 对于单个模块,您可以尝试npm install --save module@latest这也会更改package.json。 You could write a shell script or script in nodejs to iterate though package.json and update all of the modules. 您可以编写一个shell脚本或在nodejs中编写脚本,以遍历package.json并更新所有模块。

Recursive update of all modules can performed with npm update : 可以使用npm update执行所有模块的递归npm update

  • for locally installed modules: npm update --depth 9999 --dev 对于本地安装的模块: npm update --depth 9999 --dev
  • for globally installed modules: npm update --depth 9999 --dev -g 对于全局安装的模块: npm update --depth 9999 --dev -g

A ready-to-use NPM-script to update all Node.js modules with all its dependencies: 一个随时可用的NPM脚本,以更新其所有依赖项的所有Node.js模块:
How to update all Node.js modules automatically? 如何自动更新所有Node.js模块?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM