一行命令更新所有 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.0  ❯  0.22.0  https://github.com/cheeriojs/cheerio#readme
 ◯ debug     ^2.3.3   ❯  4.1.1   https://github.com/visionmedia/debug#readme
 ◯ log4js    ^1.0.1   ❯  4.1.0   https://log4js-node.github.io/log4js-node/
 ◯ mustache  ^2.3.0   ❯  3.0.1   https://github.com/janl/mustache.js
 ◯ request   2.79.0   ❯  2.88.0  https://github.com/request/request#readme
 ◯ unescape  ^0.2.0   ❯  1.0.1   https://github.com/jonschlinkert/unescape
 ◯ yargs     ^6.4.0   ❯  13.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.3   ❯  2.4.2       https://github.com/chalk/chalk#readm
e
 ◯ cheerio   latest  0.22.0  ❯  1.0.0-rc.3  https://github.com/cheeriojs/cheerio
#readme
 ◯ debug     latest  2.6.9   ❯  4.1.1       https://github.com/visionmedia/debug
#readme
 ◯ log4js    latest  1.1.1   ❯  4.1.0       https://log4js-node.github.io/log4js
-node/
 ◯ mustache  latest  2.3.2   ❯  3.0.1       https://github.com/janl/mustache.js
 ◯ request   latest  2.79.0  ❯  2.88.0      https://github.com/request/request#r
eadme
 ◯ unescape  latest  0.2.0   ❯  1.0.1       https://github.com/jonschlinkert/une
scape
 ◯ yargs     latest  6.6.0   ❯  13.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

體驗飛一般的裝包速度,再也不是裝包一小時,碼代碼五分鍾。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM