Nodejs npm 遇到 E407 代理验证错误但代理 wget 正常

不知道有人碰到没,头大。

系统:unbuntu 24.04 (刚升级的,不知道和这个有没有关系),node/npm 都是 apt 安装的,node 版本 v18.19.1 ,npm 版本 9.2.0 。

npm 无论安装什么包都提示 E407 代理验证错误。

$ npm i typestcript
npm ERR! code E407
npm ERR! 407 Proxy Authentication Required - GET https://registry.npmjs.org/typestcript

我们的确通过配置环境变量使用了代理(内网无法直接连 npm )。这个代理工作是正常的,比如 wget https://registry.npmjs.org/typestcript 都正常下载内容。

npm config set proxynpm config set https-proxy单独设置代理也都试过了。都不行。


Nodejs npm 遇到 E407 代理验证错误但代理 wget 正常
12 回复

cat ~/.npmrc
结果 po 一下
另外也发一下你的代理设置


换个国内的源吧

~/.npmrc 是空的。如果设置 npm config 之后是

<br>https-proxy=http://user123:<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="67141315061302001e27565749555349565749555352">[email&nbsp;protected]</a>:3128/<br>proxy=http://user123:<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="dfacabadbeabbab8a69feeeff1edebf1eeeff1edebea">[email&nbsp;protected]</a>:3128/<br>

但无论是否设置,都是 E407 错误。环境变量设置了 https_proxy 、all_proxy 、http_proxy ,都指向上面这个代理。

换国内源一样的错误。我的理解是代理那一层就出错了,还没访问最终目的地。

$ npm config set registry http://mirrors.cloud.tencent.com/npm/
$ npm install typescript
npm ERR! code E407
npm ERR! 407 Proxy Authentication Required - GET http://mirrors.cloud.tencent.com/npm/typescript

搜到一个可能有用的信息。我这个代理在其它还没升级到 24.04 的服务器上没问提。看来 24.04 的 ubuntu 或者新版本的 npm 升级了验证体系?

The dreaded NTLM

There is an HTTP status code 407 (proxy authentication required), which is the more correct way of saying it’s the proxy rather than the destination server that’s rejecting your request. That code plagued me for the longest time until after a lot of time on Google, I learned my proxy used NTLM authentication. HTTP basic authentication wasn’t enough to satisfy whatever proxy my corporate overlords had installed. I resorted to using Cntlm on my local machine (unauthenticated), then had it handle the NTLM authentication with the upstream proxy. Then I had to tell all the programs that couldn’t do NTLM to use my local machine as the proxy - which is generally as simple as setting HTTP_PROXY and HTTPS_PROXY. Otherwise, for npm use (as suggests):

npm config set proxy http://proxy.example.com:3128
npm config set https-proxy http://proxy.example.com:3128

一个更诡异的是,pnpm 没有问题。只有 npm 有问题。

试试改成
https-proxy=http://"user123:strategy"10.24.10.245:3128
proxy=http://"user123:strategy"10.24.10.245:3128

还真行。。。这是为什么?之前从没看见过这种写法。

不对。我搞错了。还是不行。

那只能去翻 npm 源码看他们怎么处理 url 了…

看错误是 npm 底层在去请求包的时候,走代理 proxy 的 user/pwd 没有设置生效

遇到 Node.js 的 npm 报 E407 代理验证错误,而 wget 通过同一代理正常工作的情况,通常意味着 npm 配置的代理认证信息存在问题。下面是一些排查和解决问题的步骤:

  1. 检查 npm 代理配置: 确保 npm 的代理配置正确。你可以通过以下命令查看当前的 npm 配置:

    npm config list

    特别关注 https-proxyproxy 配置项。

  2. 设置代理认证信息: 如果代理服务器需要认证,确保在 npm 配置中正确设置了用户名和密码。例如:

    npm config set https-proxy http://username:password@proxy.example.com:port
    npm config set proxy http://username:password@proxy.example.com:port

    注意,直接在命令行中输入密码存在安全风险,建议使用 .npmrc 文件配置。

  3. 使用 .npmrc 文件: 在项目的根目录或用户的主目录下创建或编辑 .npmrc 文件,添加如下内容:

    proxy=http://username:password@proxy.example.com:port/
    https-proxy=http://username:password@proxy.example.com:port/
    strict-ssl=false  # 如果代理服务器自签名证书,可能需要禁用 SSL 验证
  4. 测试连接: 配置完成后,尝试再次运行 npm 命令,如 npm install,检查是否还会出现 E407 错误。

如果以上步骤仍然无法解决问题,建议检查代理服务器的日志,以确定是否有更详细的错误信息。同时,确保 npm 和 Node.js 版本为最新,以避免潜在的已知问题。

回到顶部
AI 助手
你好,我是IT营的 AI 助手
您可以尝试点击下方的快捷入口开启体验!