一行命令更新所有 npm 依赖包

深藏阁楼爱情的钟 2022-09-13 01:59 341阅读 0赞

npm 包的更新速度很快,为了将项目或者全局依赖更新到最新版本。传统的做法是一个一个更新,比如更新 react 到最新版本,命令如下:

  1. # npm
  2. npm i --save react@latest
  3. # yarn
  4. yarn add react@latest
  5. 复制代码

yarn 是 facebook 发明的新一代 js 包管理器,支持离线使用。这是 npm 与 yarn 的 命令对照。

但是,这种做法相当耗时。有没有更简单的方法呢? 答案是使用 npm-check 或者 yarn。两者都需要全局安装。

  1. npm i -g yarn
  2. npm i -g npm-check
  3. 复制代码

使用 npm-check 更新项目依赖

在项目根目录运行

  1. npm-check -u
  2. 复制代码

输出如下:

  1. ? Choose which packages to update. (Press <space> to select)
  2. Update package.json to match version installed.
  3. ❯◯ chalk ^1.1.3 2.4.2 https://github.com/chalk/chalk#readme
  4. cheerio ^0.22.0 0.22.0 https://github.com/cheeriojs/cheerio#readme
  5. debug ^2.3.3 4.1.1 https://github.com/visionmedia/debug#readme
  6. log4js ^1.0.1 4.1.0 https://log4js-node.github.io/log4js-node/
  7. mustache ^2.3.0 3.0.1 https://github.com/janl/mustache.js
  8. request 2.79.0 2.88.0 https://github.com/request/request#readme
  9. unescape ^0.2.0 1.0.1 https://github.com/jonschlinkert/unescape
  10. yargs ^6.4.0 13.2.2 https://yargs.js.org/
  11. Space to select. Enter to start upgrading. Control-C to cancel.
  12. 复制代码

空格切换包是否更新,Control + C 取消更新,回车就是执行更新。

使用 yarn 更新项目依赖

在项目根目录运行

  1. yarn upgrade-interactive --latest
  2. 复制代码

输出如下:

  1. yarn upgrade-interactive v1.15.2
  2. info Color legend :
  3. "<red>" : Major Update backward-incompatible updates
  4. "<yellow>" : Minor Update backward-compatible features
  5. "<green>" : Patch Update backward-compatible bug fixes
  6. ? Choose which packages to update. (Press <space> to select, <a> to toggle all,
  7. <i> to invert selection)
  8. dependencies
  9. name range from to url
  10. ❯◯ chalk latest 1.1.3 2.4.2 https://github.com/chalk/chalk#readm
  11. e
  12. cheerio latest 0.22.0 1.0.0-rc.3 https://github.com/cheeriojs/cheerio
  13. #readme
  14. debug latest 2.6.9 4.1.1 https://github.com/visionmedia/debug
  15. #readme
  16. log4js latest 1.1.1 4.1.0 https://log4js-node.github.io/log4js
  17. -node/
  18. mustache latest 2.3.2 3.0.1 https://github.com/janl/mustache.js
  19. request latest 2.79.0 2.88.0 https://github.com/request/request#r
  20. eadme
  21. unescape latest 0.2.0 1.0.1 https://github.com/jonschlinkert/une
  22. scape
  23. yargs latest 6.6.0 13.2.2 https://yargs.js.org/
  24. 复制代码

yarn 提供了全选切换功能,就是按键 A,空格切换包是否更新,Control + C 取消更新,回车就是执行更新。

yarn 的更新命令太长了,谁记得住,这种时候,请合理使用命令行工具的帮助,比如运行 yarn help

更新命令对照表

更新全局依赖同上































说明 yarn npm-check
更新项目依赖,没有交互 yarn upgrade —latest npm-check -y
更新项目依赖,有交互 yarn upgrade-interactive —latest npm-check -u
更新全局依赖,没有交互 yarn global upgrade —latest npm-check -g -y
更新全局依赖,有交互 yarn global upgrade-interactive —latest npm-check -g -u

检测原理

yarn 是根据 yarn.lock 文件来检测版本是否是最新的,所以项目是使用 npm 安装依赖包,更新前要运行 yarn install 一下。

npm-check 是检测 package.json 文件,项目存在 node_modules 文件夹即可更新。

更新提醒

没有交互就是将依赖包直接更新到最新版本,推荐使用交互式更新,会有更新的警告信息。

最新的依赖包,API 可能发生重大改变。为了顺利更新,更新前请 git commit 一下,更新失败了也能顺利回退。

不推荐使用 cnpm

为了加快安装依赖的安装速度,可能被同事安利 cnpm,但是这样会导致包的依赖安装不正常,项目无法运行。

更好的做法是使用 nrm 切换下载源。

平时使用 yarn 装包,npm 运行脚本。

安装 nrm

  1. npm i -g nrm
  2. 复制代码

查看下载镜像源

  1. nrm ls
  2. 复制代码

输出如下

  1. npm ---- https://registry.npmjs.org/
  2. cnpm --- http://r.cnpmjs.org/
  3. * taobao - https://registry.npm.taobao.org/
  4. nj ----- https://registry.nodejitsu.com/
  5. npmMirror https://skimdb.npmjs.com/registry/
  6. edunpm - http://registry.enpmjs.org/
  7. 复制代码

切换镜像源

  1. nrm use taobao
  2. 复制代码

装包命令不变,比如安装 react

  1. # npm
  2. npm i --save react
  3. # yarn
  4. yarn add react
  5. 复制代码

体验飞一般的装包速度,再也不是装包一小时,码代码五分钟。

作者:nusr
链接:https://juejin.cn/post/6844903827599015944
来源:掘金
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

发表评论

表情:
评论列表 (有 0 条评论,341人围观)

还没有评论,来说两句吧...

相关阅读