日常开发反复使用npm install的时候太多了,而大多数时候它又非常的慢。所以动手研究一下如何优化。

先找到两个影响速度的瓶颈:

  1. 网络连接问题。
  2. NPM install的机制问题。

1. 解决网络连接问题。

1. 使用更快的registry镜像。方法很多主要分为三类:

  • 别名法

    1
    alias cnpm="npm --registry=https://registry.npm.taobao.org
  • 配置文件法

    1
    npm config set registry https://registry.npm.taobao.org
  • 直接用第三方的npm

    1
    2
    npm install -g cnpm --registry=https://registry.npm.taobao.org
    cnpm install

提示: 可以使用一些小工具进行切换,例如nrv

1
2
3
4
5
6
7
8
9
10
11
$npm install -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/
rednpm -- http://registry.mirror.cqupt.edu.cn
skimdb -- https://skimdb.npmjs.com/registry
$nrm use taobao

2. 使用本地缓存的npm镜像。

可能的问题:

1.无法npm publish。

因为publish频次不高,需要的时候切换回npm的官方registry就可以了。

2.自动选择问题

有的一套脚本可能会在国内和国外不同的服务器上运行,例如CI服务器在国外实际开发和部署在国内。这个时候就需要自动的选择合适的registry。

我自己写了个小程序来支持这种场景,https://github.com/guolin/ufnr

本身依赖很少下载快,并能够根据不同的环境切换合适的registry。 觉得好给个星 :)

1
2
3
$ npm install -g ufnr
$ ufnr
current registry is https://registry.npm.taobao.org

2. npm机制问题

  • 没有离线模式,必须访问网络
  • 顺序执行,效率差

解决离线和缓存的方法有:

  1. 上面提到的local-npm,相当于本地搭建一个registry镜像
  2. 替换npm install,例如,npm-cache 等
  3. –cache-min: 这个参数可以强制使用本地缓存,例如,
1
2
npm install --cache-min 999 xx
// 强制优先使用本地999分钟内的缓存

这种方法的缺点有两点;

1. 如果缓存中有对应的包依然需要发送Etag验证是否需要更新。
2. 如果缓存中有版本不对应的包则直接报错。(而不是下载正确的包)

3. 终极解决方案

yarn是facebook推出的解决方案,貌似也得到了npm的官方支持。解决了npm的主要问题,但网络连接问题依然需要切换registry来实现。