npm i 出现的网络问题

发布于:2025-03-31 ⋅ 阅读:(31) ⋅ 点赞:(0)

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等下载依赖或者插件等命令。看看是否能解决你的问题。