npm升级所有的依赖包

使用npm管理node的包,可以使用npm update <name>对单个包升级,对于npm的版本大于 2.6.1,可以使用命令:

npm install -g

升级全局的本地包。

对于版本小于2.6.1的一个一个包的升级实在是太麻烦,就想找到一个升级所有本地包的方法,找到两个比较好的方式:shell脚本npm-ckeck

shell脚本

使用shell脚本升级npm包,首先所在找到需要升级的包和版本号,再使用npm install完成升级。
npm -g是管理本地全局包的命令。通过npm -g outdated可以查看那些包有更新:

npm -g outdated

Package    Current  Wanted  Latest  Location
appium       1.5.2   1.5.3   1.5.3
bower        1.7.0   1.7.9   1.7.9
cordova      5.4.1   6.2.0   6.2.0
eslint      2.13.0   3.0.0   3.0.0
fsevents     1.0.8  1.0.12  1.0.12
grommet      0.4.1   0.6.9   0.6.9
requirejs   2.1.22   2.2.0   2.2.0

这里列出来了,当前版本,和最后的版本,只需要得到所有需要升级的包名和版本号就可以使用npm -g install <name>直接升级了。
npm -g outdated还可以使用目录的方式展示,再从中提取出包名和版本号。

npm -g outdated --parseable --depth=0

/usr/local/lib/node_modules/appium:[email protected]:[email protected]:[email protected]
...

在通过cut命令就可以得到最后要升级版本号和包名:

npm -g outdated --parseable --depth=0 | cut -d: -f2

[email protected]
.....

完整的脚本:

#!/bin/sh
set -e
#set -x
for package in $(npm -g outdated --parseable --depth=0 | cut -d: -f2)
do
    npm -g install "$package"
done

脚本下载地址:https://github.com/jjz/script/blob/master/npm-upgrade.sh

npm-check

npm-check是用来检查npm依赖包是否有更新,错误以及不在使用的,我们也可以使用npm-check进行包的更新。
安装npm-check:

npm install -g npm-check

检查npm包的状态:

npm-check -u -g

CA5E1D6E-93B8-40CA-B190-273B87364C8C.png

通过上下键可以移动光标,使用空格键可以选择需要处理的包,回车直接进行处理。
选择[email protected]包升级到3.10.3

? Choose which packages to update. [email protected]

$ npm install --global [email protected] --color=always
/usr/local/bin/npm -> /usr/local/lib/node_modules/npm/bin/npm-cli.js
/usr/local/lib
└─┬ [email protected]
  ├── [email protected]
  ├── [email protected]
  └── [email protected]

[npm-check] Update complete!
[npm-check] [email protected]
[npm-check] You should re-run your tests to make sure everything works with the updates.

通过以上两种方式可以更便利的管理本地的npm包。

参考:https://gist.github.com/othiym23/4ac31155da23962afd0e



作者:姜家志
链接:https://www.jianshu.com/p/a33702a4b096
來源:简书

猜你喜欢

转载自blog.csdn.net/xiaoguan_liu/article/details/85146437