npm 包的更新速度很快,为了将项目或者全局依赖更新到最新版本。传统的作法是一个一个更新,好比更新 react 到最新版本,命令以下:node
# npm
npm i --save react@latest
# yarn
yarn add react@latest
复制代码
yarn 是 facebook 发明的新一代 js 包管理器,支持离线使用。这是 npm 与 yarn 的 命令对照。react
可是,这种作法至关耗时。有没有更简单的方法呢? 答案是使用 npm-check 或者 yarn。二者都须要全局安装。git
npm i -g yarn
npm i -g npm-check
复制代码
在项目根目录运行github
npm-check -u
复制代码
输出以下:npm
? 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 取消更新,回车就是执行更新。json
在项目根目录运行bash
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 取消更新,回车就是执行更新。ui
yarn 的更新命令太长了,谁记得住,这种时候,请合理使用命令行工具的帮助,好比运行 yarn help。url
更新全局依赖同上
说明 | 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,可是这样会致使包的依赖安装不正常,项目没法运行。
更好的作法是使用 nrm 切换下载源。
平时使用 yarn 装包,npm 运行脚本。
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
复制代码
体验飞通常的装包速度,不再是装包一小时,码代码五分钟。