# npm - 一键更新依赖包

随着项目的迭代,依赖包可能也都升级了,如何快速的升级各种依赖到最新版本

一个一个手动升级:

# npm
npm i vue@latest
# yarn
yarn add vue@latest
1
2
3
4

批量操作

# npm-check

npm i -g npm-check
npm-check -u
1
2

输出如下:

? Choose which packages to update.
 Space to select. Enter to start upgrading. Control-C to cancel.

 Patch Update Backwards-compatible bug fixes.
>( ) vue             2.6.11   ❯  2.6.12   https://github.com/vuejs/vue#readme

 Minor Update New backwards-compatible features.
 ( ) @vue/test-utils devDep   1.0.0-beta.29  ❯  1.1.1   https://github.com/vuejs/vue-test-utils#readme
 
 Major Update Potentially breaking API changes. Use caution.
 ( ) cross-env       5.2.1   ❯  7.0.2   https://github.com/kentcdodds/cross-env#readme

 Non-Semver Versions less than 1.0.0, caution.
 ( ) axios           0.18.1  ❯  0.21.0  https://github.com/axios/axios

(Move up and down to reveal more choices)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

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

# 使用 yarn

yarn upgrade-interactive --latest
1

输出如下:

yarn upgrade-interactive v1.22.5
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)
 devDependencies
   name                    range   from       to      url
>( ) @vue/test-utils         latest  1.0.0-beta.29  ❯  1.1.1   https://github.com/vuejs/vue-test-utils#readme

 dependencies
   name                    range   from       to      url
 ( ) axios                   latest  0.18.1  ❯  0.21.0  https://github.com/axios/axios
 ( ) cross-env               latest  5.2.1   ❯  7.0.2   https://github.com/kentcdodds/cross-env#readme
 ( ) vue                     latest  2.6.11  ❯  2.6.12  https://github.com/vuejs/vue#readme
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

操作一样,空格切换是否更新,Control + C 取消更新,回车执行更新。yarn 还提供了全选,按 A。

更新命令对照表

说明 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 文件夹即可更新。

# 参考

上次更新: 11/4/2020, 10:16:01 AM