npm install 無響應解決方案

weixin_34151004發表於2016-11-16

國內訪問外網都很慢,甚至不能訪問!大家都懂,都很無奈!
安裝Node時自帶的npm地址預設是:http://registry.npmjs.org ,npm install沒反應! 跟著下面操作來解決吧!

方案一:使用cnpm

安裝

 

 

1

$ npm install cnpm -g

如果你在中國,那麼你可能需要使用這個源:淘寶 NPM 映象:

 

1

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

 

安裝模組

從 registry.npm.taobao.org 安裝所有模組. 當安裝的時候發現安裝的模組還沒有同步過來, 淘寶 NPM 會自動在後臺進行同步, 並且會讓你從官方 NPMregistry.npmjs.org 進行安裝. 下次你再安裝這個模組的時候, 就會直接從 淘寶 NPM 安裝了.

 

1

$ cnpm install [name]

 

同步(sync)模組

 

 

1

$ cnpm sync [moduleName]

 

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

方案二:使用smart-npm

智慧的 npm,讓你在中國使用 npm 時,下載速度更快,使用更方便!

背景

用 npm 時,預設它會訪問國外資源,所以會非常卡,有時甚至會被牆。現在市面上一般有三種解決方案:

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

其實 cnpm 的意圖並不是簡單給我們用來去下載 npm 資源的,它是為 cnpm 服務端(也可以理解成 npm 的私有倉庫)服務的。

所以,我們就需要一個更智慧的 npm 了,可以在我們使用 npm install 時自動從國內的映象下載,而在我們使用 npm publish 又能釋出到官方的 registry 上!

就讓 smart-npm 來為你完成吧!

安裝

 

 

1

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

如果 window 使用者安裝最新版本不成功的話,可以試試安裝 smart-npm@1 , 兩者功能差不多是一樣的, 釋出版本 2 的主要原因是由於 npm 的升級,使的在 mac 上無法通過 bin 別名的方式覆蓋原來的 npm, 只能通過先刪除原來的 npm link 檔案,再建立一個新的;但這種方式在 window 上可能會有問題, 所以,如果你是 window 使用者,並且通過上面指令碼無法安裝成功的話,可以用下面指令碼再試試。

 

1

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

安裝成功後預設會在你的 npm 使用者配置檔案 ~/.npmrc 中新增淘寶的 registry。

解除安裝

 

 

1

2

npm smart uninstall   # 2.x.x 版本的 smart-npm 在解除安裝前需要先執行此指令碼

npm uninstall --global smart-npm

要先執行 npm smart uninstall 是因為如果直接執行 npm uninstall 會導致找不到 npm 檔案

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

 

 

1

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

 

使用

  • 安裝後系統的 npm 會被替換了,如果你要使用原生的 npm 命令,可以用 npm-original 代替。
  • 新的 npm 會自動根據你使用的命令切換 registry:當你使用 publish, config, adduser, star 等(click here to see more) 命令時,會強制使用官方的 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

方案三:使用nrm

nrm 是一個 NPM 源管理器,允許你快速地在如下 NPM 源間切換,現已支援now include: npm, cnpm, taobao,nj(nodejitsu), rednpm。

nrm安裝

 

 

1

$ npm install -g nrm

 

示例

 

 

1

2

3

4

5

6

7

8

$ 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

 

 

1

2

3

$ nrm use cnpm  //switch registry to cnpm

 

    Registry has been set to: http://r.cnpmjs.org/

 

使用

 

 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

Usage: nrm [options] [command]

 

  Commands:

 

    ls                           List all the registries

    use                Change registry to registry

    add   [home]  Add one custom registry

    del                Delete one custom registry

    home  [browser]    Open the homepage of registry with optional browser

    test [registry]              Show the response time for one or all registries

    help                         Print this help

 

  Options:

 

    -h, --help     output usage information

    -V, --version  output the version number

增加源:

 

1

nrm add [home]

刪除源:

 

1

nrm del

測試速度:

 

1

nrm test

注意: nrm只是一個源管理器,也不能使用publish命令。

以上三種方案均可解決npm install 慢npm install 無響應npm install 無法安裝的問題。

參考

https://npm.taobao.org
https://github.com/cnpm/cnpm
https://github.com/qiu8310/smart-npm/
https://github.com/Pana/nrm
https://segmentfault.com/a/1190000002642514

原文連線:npm install 無響應解決方案,轉載請註明出自體驗盒子 | 關注網路安全

Tags: npm , npm install

轉載於:https://my.oschina.net/pvpCC9IFwqz4/blog/789238

相關文章