npm i 出现的网络问题

npm i 出现的网络问题

解决方案:

  1. npm config list 查看.npmrc文件中是否配置了proxy
  2. 删除.npmrc文件中的proxy,保存。
  3. 重新执行npm i命令。

顺便说说解决这个问题的心里路程

每次安装vue的环境的时候,经常遇到npm安装一些插件或者是依赖的时候报的一个网络连接的问题导致的安装失败。

如:

复制代码
➜ npm i
npm error code ECONNRESET
npm error syscall read
npm error errno ECONNRESET
npm error network Invalid response body while trying to fetch https://repo.huaweicloud.com/repository/npm/yargs-parser: read ECONNRESET
npm error network This is a problem related to network connectivity.
npm error network In most cases you are behind a proxy or have bad network settings.
npm error network
npm error network If you are behind a proxy, please make sure that the
npm error network 'proxy' config is set properly.  See: 'npm help config'
npm error A complete log of this run can be found in: /Users/laizhaobin/.npm/_logs/2025-03-29T09_28_10_433Z-debug-0.log

网上找了很多资料发现都不能解决问题。

复制代码
➜  workspace_vue npm install -g cnpm --registry=https://registry.npm.taobao.org
npm error code CERT_HAS_EXPIRED
npm error errno CERT_HAS_EXPIRED
npm error request to https://registry.npm.taobao.org/cnpm failed, reason: certificate has expired
npm error A complete log of this run can be found in: /Users/xxx/.npm/_logs/2025-03-29T11_33_27_868Z-debug-0.log

甚至尝试去安装cnpm也是失败的。

在没有办法的情况下,只能自己不断的去尝试一些可能的方法了。

先细读log发现。

error network If you are behind a proxy, please make sure that the
error network 'proxy' config is set properly. See: 'npm help config'

然后尝试去查看npm的配置文件

查看npm的配置文件

复制代码
npm config list

执行的结果如下:

复制代码
➜  npm config list
; "user" config from /Users/xxxxx/.npmrc

proxy = "http://proxy.server:port"
registry = "https://registry.npmjs.org/"

; node bin location = /usr/local/bin/node
; node version = v22.14.0
; npm local prefix = /xxxxx/
; npm version = 10.9.2
; cwd = /xxxxxxxx
; HOME = /xxxxxxxx
; Run `npm config ls -l` to show all defaults.

这里有个关键信息:
proxy = "http://proxy.server:port"

看到这个之后我就在想是不是因为这个导致的。于是,我就大胆的把它删了。

删除proxy

从 上面的打印出来的日志里可以看到config的文件地址是:/Users/xxx/.npmrc

复制代码
~ open /Users/xxx/.npmrc

这个时候就打开一个内容如下的文件

复制代码
proxy = http://proxy.server:port
registry=https://registry.npmjs.org/

直接手动删除,然后Command + S 保存一下这个文件。

最后重新去执行你之间想执行的npm i等下载依赖或者插件等命令。看看是否能解决你的问题。

相关推荐
亦黑迷失4 分钟前
map + Promise.all 实现按顺序获取并发请求的结果
前端·javascript·promise
i编程_撸码5 分钟前
React18+ 项目搭建-从初始化、技术选型到开发部署的全流程规划
前端
天才熊猫君5 分钟前
为什么 flex: 1 的效果和预想的不一样?
前端
七月丶8 分钟前
❌「不要再封装组件了!」我把 UI 重构成了纯函数式
前端·javascript·后端
天才首富科学家9 分钟前
Web前端(11)-vue刷新网页404、登录状态页面路由处理
前端·vue.js
天才首富科学家10 分钟前
Web前端(12)-vue代码读取package.json中的version
前端·vue.js
zayyo10 分钟前
避免页面卡顿:一文掌握 Web Workers 使用指南
前端·javascript·面试
海底火旺10 分钟前
从暴力排序到滑动窗口:解决字母异位词搜索的优化之路
前端·算法·面试
CF14年老兵10 分钟前
📚 API 设计终极指南:从基础到进阶
前端·后端·设计模式
爱生活的前端狗11 分钟前
配置 eslint 也太容易了
前端·javascript·vue.js