方案一:使用cnpm
安裝:
npm install cnpm -g
或許你在中國,那么你可以能需要使用這個源:淘寶NPM鏡像
npm install cnpm -g --registry=https://registry.npm.taobao.org
安裝模塊
從registry.npm.taobao.org安裝的所有模塊,當安裝的時候發現安裝的模塊還沒有同步過來,淘寶NPM會自動在后台進行同步,並且會讓你從官方NPM 進行安裝。下次你再安裝這個模塊的時候,就會直接從淘寶 NPM 安裝了。
cnpm install [name]
同步模塊
cnpm sync [moduleName]
注意:cnpm支持npm除了publish之外的所有命令 ,也就是不支持publish,當然這並不影響我們使用,publish時換回npm即可。
方案二:使用smart-npm
用npm時,默認他會訪問國外的資源,所以會非常卡,有時甚至會被牆。現在市面上一般有三種解決方案:
- 在.npmrc上配置一個國內的registry鏡像。
- 使用cnpm。
- 使用VPN
方案一,很粗暴,可以解決很多下載慢的問題,但是當你用npm publish時就會失敗
方案二,不錯,但這樣又會遇到問題 ,到底哪些命令需要用cnpm哪些命令需要用npm呢?
方案三,有時也不能百分百解決問題,有時有些VPN也不穩定,但有個VPN很保險就是。
其實cnpm的意圖並不是簡單給我們用來去下載的npm資源的,他是為cnpm服務端服務的。
所以,我們就需要一個更智能的npm了,可以在我們使用npm install 時自動從國內的鏡像下載,而在我們使用npm publish 又能發布到官方的registry上。
這樣就讓 smart-npm來完成吧!
安裝
npm install --global smart-npm --registry=https://registry.npm.taobao.org/
如果window用戶安裝最新版本不成功的話,可以試試安裝smart-npm@1
npm install --global smart-npm@1 --registry=https://registry.npm.taobao.org/
安裝成功后默認會在你的npm用戶配置文件 ~/.npmrc中添加淘寶的registry。
卸載:
npm smart uninstall # 2.x.x 版本的 smart-npm 在卸載前需要先執行此腳本 npm uninstall --global smart-npm
先執行 npm smart uninstall 是因為如果直接執行npm uninstall 會導致找不到npm文件。
使用:
- 安裝后系統的npm 會被替換了,如果你要是用原生的npm命令,可以用 npm-original 代替。
- 新的npm 會自動根據你使用的命令切換 registry。
- 當你使用publish、config、adduser、star、等命令式,會強制使用官方的registry
- 當你使用其他命令時,都會使用淘寶的鏡像
- 如果要強制使用某個registry時,只要在命令后面添加 registry參數即可,例如:
npm install jquery --registry=https://r.cnpmjs.org
就會使用你指定的registry去拉取
- 如果要強制使用官方的registry,只要在命令后面加上 --npm 即可,比如
npm install jquery --npm
就會使用官方的registry去拉取jquery
方案三:使用nrm
nrm 是一個NPM 資源管理器
安裝:
npm install -g nrm
使用:
nrm add [home]
刪除源
nrm del
測試速度
nrm test
備注
npm---https://registry.npmjs.org
cnpm ---- http://r.cnpmjs.org/
taobao -- https://registry.npm.taobao.org/
