npm i 出現的網絡問題
解決方案:
- npm config list 查看.npmrc文件中是否配置了proxy
- 刪除.npmrc文件中的proxy,保存。
- 重新執行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/.npmrcproxy = "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等下載依賴或者插件等命令。看看是否能解決你的問題。