npm install 安裝過程卡住不動,無反應,或無法安裝

說明:安裝Node 時默認下載地址是http://registry.npmjs.org

解決方案

1.在 .npmrc 上配置一個國內的 registry 鏡像  
2.使用 cnpm
3.使用 VPN
第1個方案很粗暴,可以解決很多下載慢的問題,但是當你用 npm publish 時就會失敗
第2個方案不錯,但這樣你就又會遇到問題,到底哪些命令需要用 cnpm,哪些命令需要用 npm 呢?
VPN 方案有時也不能百分百解決問題,有時有些 VPN 也不穩定,但有個 VPN 很保險就是
 

推薦方案:使用smart-npm   

(可以在我們使用 npm install 時自動從國內的鏡像下載,而在我們使用 npm publish 又能發佈到官方的 registry 上)

安裝

npm install --global smart-npm --registry=https://registry.npm.taobao.org/

這個命令不行可以使用下面這兩個

npm install --global smart-npm@1 --registry=https://registry.npm.taobao.org/
npm install --global smart-npm@2 --registry=https://registry.npm.taobao.org/


卸載

npm smart uninstall   # 2.x.x 版本的 smart-npm 在卸載前需要先執行此腳本
npm uninstall --global smart-npm


要先執行 npm smart uninstall 是因爲如果直接執行 npm uninstall 會導致找不到 npm 文件

Mac 或 Linux 用戶可以使用下面命令恢復之前備份的 npm

mv $ (whichnpm-original) $ (dirname$(whichnpm-original))/npm


使用

① 使用原生的 npm 命令,可以用 npm-original 代替(安裝後系統的 npm 會被替換)
新的 npm 會自動根據你使用的命令切換 registry:當你使用 publish, config, adduser, star 等命令時,會強制使用官方的 registry: https://registry.npmjs.org;當你使用其它命令時,都會使用淘寶的鏡像https://registry.npm.taobao.org/


②強制使用某個 registry 時,只要在命令後面添加 registry 參數即可,

比如, npm install jquery --registry=https://r.cnpmjs.org 就會使用你指定的 registry 去拉取 jquery


③強制使用官方的 registry, 只要在命令後面加上 --npm 即可

比如, npm install jquery --npm 就會使用官方的 registry 去拉取 jquery,(當鏡像沒有及時更新時,用此會選項很有效)


④修改默認的淘寶鏡像或者官方的 registry

可以在你的環境變量中添加這兩個參數:

NPM_OFFICIAL_REGISTRY, NPM_MIRROR_REGISTRY

以此來修改默認的官方 registry 和 淘寶鏡像 registry。 


本地安裝也會替代了全局的 npm

 

方案二:使用cnpm


安裝CNPM

$ npm install cnpm -g


CNPM可以使用這個鏡像:淘寶 NPM 鏡像:

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


使用CNPM 下載

$ cnpm install [name]


同步(sync)模塊

$cnpm sync [moduleName]


注意:cnpm支持 npm 除了 publish 之外的所有命令,也就是不支持publish,當然這並不影響我們使用,publish時換回npm即可,這樣也能解決npm install無響應的問題

 

拓展:nrm(一個源管理器,便捷切換源)

 

nrm安裝

$ 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 cnpm  //這是選擇cnpm作爲源
 
Registry has been set to: http://r.cnpmjs.org/


使用
 

Usage:nrm[options][command]
 
  Commands:
 
    ls                          Listalltheregistries
    use                Changeregistrytoregistry
    add  [home]  Addonecustomregistry
    del                Deleteonecustomregistry
    home  [browser]    Openthehomepageofregistrywithoptionalbrowser
    test[registry]              Showtheresponsetimeforoneorallregistries
    help                        Printthishelp
 
  Options:
 
    -h,--help    outputusageinformation
    -V,--version  outputtheversionnumber


增加源

nrm add [home]


刪除源

nrm del


測試速度

nrm test


注意: nrm也不能使用publish命令
 

參考鏈接 https://blog.csdn.net/fanhu6816/article/details/53812724

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章