npm install慢/无响应解决方案

简介: npm install慢/无响应解决方案

npm install慢/无响应解决方案

国内访问外网都很慢,甚至不能访问!大家都懂,都很无奈!

安装Node时自带的npm地址默认是:http://registry.npmjs.orgnpm install没反应! 跟着下面操作来解决吧!

方案一:使用cnpm

安装

$ npm install cnpm -g
• 1

如果你在中国,那么你可能需要使用这个源:淘宝 NPM 镜像:

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

安装模块

registry.npm.taobao.org 安装所有模块. 当安装的时候发现安装的模块还没有同步过来, 淘宝 NPM 会自动在后台进行同步, 并且会让你从官方 NPMregistry.npmjs.org 进行安装. 下次你再安装这个模块的时候, 就会直接从 淘宝 NPM 安装了.

$ cnpm install [name]
• 1

同步(sync)模块

$ cnpm sync [moduleName]
• 1

注意: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 来为你完成吧!

安装

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

如果 window 用户安装最新版本不成功的话,可以试试安装 smart-npm@1 , 两者功能差不多是一样的, 发布版本 2 的主要原因是由于 npm 的升级,使的在 mac 上无法通过 bin 别名的方式覆盖原来的 npm, 只能通过先删除原来的 npm link 文件,再创建一个新的;但这种方式在 window 上可能会有问题, 所以,如果你是 window 用户,并且通过上面脚本无法安装成功的话,可以用下面脚本再试试。

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

安装成功后默认会在你的 npm 用户配置文件 ~/.npmrc 中添加淘宝的 registry。

卸载

npm smart uninstall   # 2.x.x 版本的 smart-npm 在卸载前需要先执行此脚本
npm uninstall --global smart-npm
• 1
• 2

要先执行 npm smart uninstall 是因为如果直接执行 npm uninstall 会导致找不到 npm 文件

Mac 或 Linux 用户可以使用下面命令恢复之前备份的 npm

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

使用

  • 安装后系统的 npm 会被替换了,如果你要使用原生的 npm 命令,可以用 npm-original 代替。
  • 新的 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

方案三:使用nrm

nrm 是一个 NPM 源管理器,允许你快速地在如下 NPM 源间切换,现已支持now include: npm, cnpm, taobao,nj(nodejitsu), rednpm。

nrm安装

$ npm install -g nrm
• 1

示例

$ 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
• 4
• 5
• 6
• 7
• 8
$ nrm use cnpm  //switch registry to cnpm
    Registry has been set to: http://r.cnpmjs.org/
• 1
• 2
• 3

使用

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
• 2
• 3
• 4
• 5
• 6
• 7
• 8
• 9
• 10
• 11
• 12
• 13
• 14
• 15
• 16

增加源:

nrm add [home]
• 1

删除源:

nrm del
• 1

测试速度:

nrm test
• 1

注意: 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

相关文章
|
1月前
|
网络安全 计算机视觉
【node】 npm install 报错:code 128
【node】 npm install 报错:code 128
40 1
|
1月前
|
JavaScript Windows
npm install安装太慢或者失败,可以尝试一下以下方法
npm install安装太慢或者失败,可以尝试一下以下方法
86 1
|
3天前
|
JavaScript
npm install没问题,但npm run dev的时候报Node Sass version 6.0.1 is incompatible with ^4.0.0 ^5.0.0
npm install没问题,但npm run dev的时候报Node Sass version 6.0.1 is incompatible with ^4.0.0 ^5.0.0
|
2月前
|
缓存
npm install 报 npm ERR! cb()never called!的错误
npm install 报 npm ERR! cb()never called!的错误
20 0
|
1月前
|
Linux
npm install 报错ERESOLVE unable to resolve dependency tree
npm install 报错ERESOLVE unable to resolve dependency tree
15 0
|
5天前
npm install 太慢?解决方法
npm install 太慢?解决方法
21 0
|
14天前
使用npm install时遇到问题:npm ERR! code ERESOLVE
使用npm install时遇到问题:npm ERR! code ERESOLVE
|
25天前
|
缓存 资源调度
npm install安装时一直idealTree:npm: sill idealTree buildDeps解决方案(亲测有效)
npm install安装时一直idealTree:npm: sill idealTree buildDeps解决方案(亲测有效)
51 2
|
1月前
|
JavaScript 开发者 资源调度
Spartacus 2211 开发版本采用 npm install 结合 package-lock.json 避免 build 出错
Spartacus 2211 开发版本采用 npm install 结合 package-lock.json 避免 build 出错
10 0
Spartacus 2211 开发版本采用 npm install 结合 package-lock.json 避免 build 出错
|
2月前
|
内存技术
node版本与npm版本不对应的解决方案
node版本与npm版本不对应的解决方案
29 0

推荐镜像

更多