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

269 天前
 zhiqiang

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

系统: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单独设置代理也都试过了。都不行。

1315 次点击
所在节点    Node.js
11 条回复
NessajCN
269 天前
cat ~/.npmrc
结果 po 一下
另外也发一下你的代理设置
arfaWong
269 天前
换个国内的源吧
zhiqiang
269 天前
@NessajCN ~/.npmrc 是空的。如果设置 npm config 之后是

```
https-proxy=http://user123:strategy@10.24.10.245:3128/
proxy=http://user123:strategy@10.24.10.245:3128/
```

但无论是否设置,都是 E407 错误。环境变量设置了 https_proxy 、all_proxy 、http_proxy ,都指向上面这个代理。
zhiqiang
269 天前
@arfaWong 换国内源一样的错误。我的理解是代理那一层就出错了,还没访问最终目的地。

$ 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
zhiqiang
269 天前
搜到一个可能有用的信息。我这个代理在其它还没升级到 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 @Agus suggests):

npm config set proxy http://proxy.example.com:3128
npm config set https-proxy http://proxy.example.com:3128
zhiqiang
269 天前
一个更诡异的是,pnpm 没有问题。只有 npm 有问题。
NessajCN
269 天前
@zhiqiang 试试改成
https-proxy=http://"user123:strategy"@10.24.10.245:3128
proxy=http://"user123:strategy"@10.24.10.245:3128
zhiqiang
269 天前
@NessajCN 还真行。。。这是为什么?之前从没看见过这种写法。
zhiqiang
269 天前
不对。我搞错了。还是不行。
NessajCN
269 天前
@zhiqiang 那只能去翻 npm 源码看他们怎么处理 url 了...
former
268 天前
看错误是 npm 底层在去请求包的时候,走代理 proxy 的 user/pwd 没有设置生效

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/1038941

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX