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

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

# npm  npm i --save react@latest  # yarn  yarn add react@latest

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

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

npm i -g yarn  npm i -g npm-check

使用 npm-check 更新项目依赖

在项目根目录运行

npm-check -u

输出如下:

? Choose which packages to update. (Press <space> to select)     Update package.json to match version installed.  ❯◯ chalk     ^1.1.3   ❯  2.4.2   https://github.com/chalk/chalk#readme   ◯ cheerio   ^0.22.00.22.0  https://github.com/cheeriojs/cheerio#readme   ◯ debug     ^2.3.34.1.1   https://github.com/visionmedia/debug#readme   ◯ log4js    ^1.0.14.1.0   https://log4js-node.github.io/log4js-node/   ◯ mustache  ^2.3.03.0.1   https://github.com/janl/mustache.js   ◯ request   2.79.02.88.0  https://github.com/request/request#readme   ◯ unescape  ^0.2.01.0.1   https://github.com/jonschlinkert/unescape   ◯ yargs     ^6.4.013.2.2  https://yargs.js.org/     Space to select. Enter to start upgrading. Control-C to cancel.

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

使用 yarn 更新项目依赖

在项目根目录运行

yarn upgrade-interactive  --latest

输出如下:

yarn upgrade-interactive v1.15.2  info Color legend :   "<red>"    : Major Update backward-incompatible updates   "<yellow>" : Minor Update backward-compatible features   "<green>"  : Patch Update backward-compatible bug fixes  ? Choose which packages to update. (Press <space> to select, <a> to toggle all,  <i> to invert selection)   dependencies     name      range   from       to          url  ❯◯ chalk     latest  1.1.32.4.2       https://github.com/chalk/chalk#readm  e   ◯ cheerio   latest  0.22.01.0.0-rc.3  https://github.com/cheeriojs/cheerio  #readmedebug     latest  2.6.94.1.1       https://github.com/visionmedia/debug  #readme   ◯ log4js    latest  1.1.14.1.0       https://log4js-node.github.io/log4js  -node/   ◯ mustache  latest  2.3.23.0.1       https://github.com/janl/mustache.js   ◯ request   latest  2.79.02.88.0      https://github.com/request/request#r  eadme   ◯ unescape  latest  0.2.01.0.1       https://github.com/jonschlinkert/une  scape   ◯ yargs     latest  6.6.013.2.2      https://yargs.js.org/

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

npm i -g nrm 

查看下载镜像

nrm ls

输出如下

  npm ---- https://registry.npmjs.org/    cnpm --- http://r.cnpmjs.org/  * taobao - https://registry.npm.taobao.org/    nj ----- https://registry.nodejitsu.com/    npmMirror  https://skimdb.npmjs.com/registry/    edunpm - http://registry.enpmjs.org/

切换镜像源

nrm use taobao

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

# npm  npm i --save react  # yarn  yarn add react

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

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

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

栗子博客 软件 一行命令更新所有 npm 依赖包 https://www.lizi.tw/soft/14225.html

常见问题
  • 1、杰齐1.7仅适用于PHP5.2 2、需Zend支持 3、尽量使用宝塔面板 4、尽量使用Windows 系统,关关对Linux支持不太友好。
查看详情

相关文章

评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务

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

分享本文封面