npm install 安装过程卡住不动,无反应,或无法安装

本文针对npm install卡住不动或无法安装的问题,提供了两种解决方案:使用smart-npm自动选择镜像源,以及使用cnpm进行安装。smart-npm在npm install时自动从国内镜像下载,npm publish时则发布到官方registry。而cnpm作为npm的替代,支持除publish外的所有命令。此外,还介绍了nrm工具用于方便地切换npm源。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

说明:安装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.youkuaiyun.com/fanhu6816/article/details/53812724

### 解决方案 当在 VS Code 终端运行 `npm install hy-event-store` 命令时如果遇到无响应的情况,可能由多种原因引起。以下是可能导致此现象的原因及其解决方案: #### 1. 网络连接问题 网络不稳定可能会导致 npm 安装过程卡住。可以尝试更换镜像源来加速下载速度。 ```bash npm config set registry https://registry.npmmirror.com/ ``` 设置完成后再次执行安装命令[^1]。 #### 2. 资源占用过高 某些情况下,系统资源不足也可能导致 npm 进程无法正常工作。可以通过以下方式排查并解决问题: - 关闭不必要的应用程序以释放内存和 CPU。 - 使用任务管理器查看是否有其他高负载进程影响当前操作。 #### 3. Node.js 和 NPM 版本不匹配 确保使用的 Node.js 及其配套的 NPM 是兼容版本。过旧者最新预览版都可能存在未知 bug 导致异常行为发生。建议升级到稳定长期支持 (LTS) 的 release 来获得更好的稳定性保障[^2]。 更新 nodejs 方法如下所示: ```bash nvm install --lts nvm use --lts ``` 对于未安装 nvm 用户,则需访问官网获取对应平台二进制包自行替换原有文件夹内容完成手动迁移流程[^3]。 #### 4. 缓存损坏 有时缓存数据出现问题也会引发类似的错误状况。清除本地缓存后再重试往往能有效缓解此类难题。 清理指令如下所列形式呈现出来供参考采用 : ```bash npm cache clean --force ``` 之后再重新发起依赖项加载请求即可恢复正常运转状态[^4]。 --- ### 注意事项 以上提到的各种措施均应在具备一定基础知识前提下谨慎施行以免造成不可逆损害后果;另外还需注意备份重要资料以防万一意外丢失情况出现带来额外麻烦困扰等问题产生风险隐患存在可能性较大因此务必小心行事遵循指导原则逐步推进直至最终成功解决目标达成为止方为上策良计之选也!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值