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 即可

扫描二维码关注公众号,回复: 11310532 查看本文章

比如, 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

猜你喜欢

转载自blog.csdn.net/qq_34823218/article/details/106845256