日常开发重复使用npminstall的次数太多了,而且大部分时间都很慢。所以动手弄清楚如何优化。首先找到两个影响速度的瓶颈:网络连接问题。npminstall的机制问题。1.解决网络连接问题。使用更快的注册表镜像。很多方法主要分为三类:别名方法aliascnpm="npm--registry=https://registry.npm.taobao.org配置文件方法npmconfigsetregistryhttps://registry.npm.taobao.orgdirect使用第三方npmnpminstall-gcnpm--registry=https://registry.npm.taobao.orgcnpminstallTips:可以使用一些小工具来切换,比如nrv$npminstall-gnrm$nrmls*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.cnskimdb--https://skimdb.npmjs.com/registry$nrm使用淘宝使用本地缓存npmmirror.npm-proxy-cachelocal-npm:https://github.com/nolanlawso...npm-lazy可能存在的问题:1.无法在npm上发布,因为发布频率不高,切换回需要时npm官方注册2.自动选择问题,有些脚本可能运行在不同的国内外服务器上。比如CI服务器,其实是国外开发,国内部署的。这时候就需要自动选择合适的注册表。我自己写了一个小程序来支持这个场景。https://github.com/guolin/ufnr本身依赖下载量少,可以根据不同环境切换合适的registry。我觉得给个star很好:)$npminstall-gufnr$ufnrcurrentregistry是https://registry.npm.taobao.org2。npm机制问题没有离线模式,必须接入网络顺序执行,效率较差。解决离线和缓存的方法是的:上面说的local-npm相当于在本地建立一个registry镜像来代替npminstall,比如npm-cache等--cache-min:这个参数可以强制使用本地缓存,比如npminstall--cache-min999xx//999分钟内强制使用本地缓存首先有两个缺点;1、如果缓存中有对应的包,还是需要发送Etag来验证是否需要更新。2.如果缓存中有版本不对应的包,会直接报错。(而不是下载正确的包)3.终极解决方案yarn是facebook推出的一个解决方案,貌似是npm官方支持的。npm的主要问题解决了,但是网络连接问题还是需要切换注册表来实现。
