npm install 会抛出 ECONNRESET 错误,但仅限于某些软件包

问题描述 投票:0回答:2

我在公司代理后面工作。我的 npm 配置如下:

$ npm config get

...
https_proxy = "http://proxy.my-domain.com:8080" 
https-proxy = "http://proxy.my-domain.com:8080" 
proxy = "http://proxy.my-domain.com:8080"
... 

通过这些设置,我可以很好地安装一些软件包,但不能安装其他软件包。例如,

$ npm i react
工作得很好,而安装
@babel/core
会引发 ECONNRESET 错误。

$ npm i @babel/core

npm ERR! code ECONNRESET
npm ERR! syscall read
npm ERR! errno -54
npm ERR! network read ECONNRESET
...

奇怪的是,我可以使用yarn安装包(它的代理配置方式与npm相同),但它也告诉我我有网络问题(即使它成功安装了包🤔)

$ yarn add @babel/core
...
✨  Done in 2.99s.
info There appears to be trouble with your network connection. Retrying...
info There appears to be trouble with your network connection. Retrying...
info There appears to be trouble with your network connection. Retrying...

我不知道为什么它适用于yarn,但不适用于npm。这是 npm 调试日志的样子:

73 silly tarball no local data for @jridgewell/sourcemap-codec@https://registry.npmjs.org/@jridgewell/sourcemap-codec/-/sourcemap-codec-1.4.14.tgz. Extracting by manifest.
74 silly tarball no local data for @jridgewell/trace-mapping@https://registry.npmjs.org/@jridgewell/trace-mapping/-/trace-mapping-0.3.17.tgz. Extracting by manifest.
75 silly tarball no local data for @jridgewell/set-array@https://registry.npmjs.org/@jridgewell/set-array/-/set-array-1.1.2.tgz. Extracting by manifest.
76 verbose stack Error: read ECONNRESET
76 verbose stack     at TLSWrap.onStreamRead (node:internal/stream_base_commons:217:20)
77 verbose cwd /Users/justinsmith/Dev/test-npm
78 verbose Darwin 21.4.0
79 verbose node v18.9.0
80 verbose npm  v8.19.1
81 error code ECONNRESET
82 error syscall read
83 error errno -54

有人知道为什么会发生这种情况吗?

npm npm-install
2个回答
1
投票

tl;dr 将其添加到我的 ~./npmrc 中提供了临时修复:

# This is what I already had set
proxy=http://proxy.my-domain.com:8080/
https-proxy=http://proxy.my-domain.com:8080/
https_proxy=http://proxy.my-domain.com:8080/

# This is what I added
strict-ssl=false
registry=http://registry.npmjs.org/
maxsockets=3

这迫使 npm 使用 http 而不是 https 发出请求,并将并行请求的数量限制为三个。

此方法有效的一个原因是,显然公司代理在将文件转发给客户端之前会扫描文件是否存在恶意软件。显然,npm 通过 TLS 并行发出太多请求会压垮恶意软件扫描程序,最终拒绝某些连接。

另一方面,Yarn 会重试几次,而 npm 只是抛出错误并放弃。

长期解决方案是让您的 IT 部门将 npm 添加到恶意软件扫描排除列表中。

完全归功于这篇博客文章,它提供了这些有用的解决方案。


0
投票

我尝试过:npm config set Legacy-peer-deps true 它解决了错误,我轻松安装了依赖项

© www.soinside.com 2019 - 2024. All rights reserved.