node.js 代理后面的 npm 失败,状态为 403

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11773509/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 16:07:27  来源:igfitidea点击:

npm behind a proxy fails with status 403

httpnode.jsproxynpm

提问by nwinkler

I'm trying to run npmbehind a proxy. I've tried both entering the proxy directly or through Authoxy:

我正在尝试在代理后面运行npm。我试过直接或通过Authoxy进入代理:

npm config set proxy http://localhost:8999
npm config set https-proxy http://localhost:8999

Regardless of which proxy I use, I always end up with the same error when running npm search:

无论我使用哪个代理,我在运行时总是遇到相同的错误npm search

npm info it worked if it ends with ok
npm verb cli [ 'node', '/usr/local/bin/npm', 'search' ]
npm info using [email protected]
npm info using [email protected]
npm verb config file /Users/xxx/.npmrc
npm verb config file /usr/local/etc/npmrc
npm verb config file /usr/local/lib/node_modules/npm/npmrc
npm WARN Building the local index for the first time, please be patient
npm verb url raw /-/all
npm verb url resolving [ 'https://registry.npmjs.org/', './-/all' ]
npm verb url resolved https://registry.npmjs.org/-/all
npm info retry registry request attempt 1 at 09:48:47
npm http GET https://registry.npmjs.org/-/all
npm info retry will retry, error on last attempt: Error: tunneling socket could not be established, sutatusCode=403
npm info retry registry request attempt 2 at 09:48:57
npm http GET https://registry.npmjs.org/-/all
npm info retry will retry, error on last attempt: Error: tunneling socket could not be established, sutatusCode=403
npm info retry registry request attempt 3 at 09:49:57
npm http GET https://registry.npmjs.org/-/all
npm ERR! Error: tunneling socket could not be established, sutatusCode=403
npm ERR!     at ClientRequest.onConnect (/usr/local/lib/node_modules/npm/node_modules/request/tunnel.js:148:19)
npm ERR!     at ClientRequest.g (events.js:185:14)
npm ERR!     at ClientRequest.EventEmitter.emit (events.js:115:20)
npm ERR!     at Socket.socketOnData (http.js:1383:11)
npm ERR!     at TCP.onread (net.js:410:27)

The command always fails with sutatusCode[sic!] 403- which means unauthorized. I have set up Authoxy to not require a username/password. The same error happens when I bypass Authoxy and provide the real proxy credentials for our NTLM proxy in the form of http:// user:pass@proxy:port.

该命令总是以sutatusCode[sic!] 403失败- 这意味着未经授权。我已将 Authoxy 设置为不需要用户名/密码。当我绕过 Authoxy 并以 http://user:pass@proxy:port 的形式为我们的 NTLM 代理提供真实代理凭据时,会发生同样的错误。

How can I make this work through the proxy?

我怎样才能通过代理完成这项工作?

Update

更新

I have created an issue on the NPM project to report this: https://github.com/isaacs/npm/issues/2866

我在 NPM 项目上创建了一个问题来报告这个:https: //github.com/isaacs/npm/issues/2866

回答by nwinkler

OK, so within minutes after posting the question, I found the answer myself here: https://github.com/npm/npm/issues/2119#issuecomment-5321857

好的,所以在发布问题后几分钟内,我在这里找到了自己的答案:https: //github.com/npm/npm/issues/2119#issuecomment-5321857

The issue seems to be that npm is not that great with HTTPS over a proxy. Changing the registry URL from HTTPS to HTTP fixed it for me:

问题似乎是 npm 在代理上使用 HTTPS 并不是那么好。将注册表 URL 从 HTTPS 更改为 HTTP 为我修复了它:

npm config set registry http://registry.npmjs.org/

I still have to provide the proxy config (through Authoxy in my case), but everything works fine now.

我仍然需要提供代理配置(在我的情况下通过 Authoxy),但现在一切正常。

Seems to be a common issue, but not well documented. I hope this answer here will make it easier for people to find if they run into this issue.

似乎是一个常见问题,但没有很好的记录。我希望这里的这个答案能让人们更容易找到他们是否遇到这个问题。

回答by tedyyu

npm config set proxy http://proxy.company.com:8080
npm config set https-proxy http://proxy.company.com:8080

credit goes to http://jjasonclark.com/how-to-setup-node-behind-web-proxy.

归功于http://jjasonclark.com/how-to-setup-node-behind-web-proxy

回答by Olivier C

If you need to provide a username and password to authenticate at your proxy, this is the syntax to use:

如果您需要提供用户名和密码以在您的代理上进行身份验证,请使用以下语法:

npm config set proxy http://usr:pwd@host:port
npm config set https-proxy http://usr:pwd@host:port

回答by atlMapper

If anyone else ends up breaking their proxy config settings go to your .npmrc, to type in the settings. This file is located at your node root folder level.

如果其他人最终破坏了他们的代理配置设置,请转到您的.npmrc,输入设置。此文件位于您的节点根文件夹级别。

Here's whats my corrected file looks like:

这是我更正后的文件的样子:

#proxy = http://proxy.company.com:8080
https-proxy = https://proxy.company.com:8080 
registry = http://registry.npmjs.org/

回答by Marc.S

For those using Jenkins or other CI server: it matters whereyou define your proxies, especially when they're different in your local development environment and the CI environment. In this case:

对于那些使用 Jenkins 或其他 CI 服务器的人:在何处定义代理很重要,尤其是当它们在本地开发环境和 CI 环境中不同时。在这种情况下:

  • don't define proxies in project's .npmrc file. Or if you do, be sure to override the settings on CI server.
  • any other proxy settings might cause 403 Forbiddenwith little hint to the fact that you're using the wrong proxy. Check your gradle.propertiesor such and fix/override as necessary.
  • 不要在项目的 .npmrc 文件中定义代理。或者,如果您这样做,请确保覆盖 CI 服务器上的设置。
  • 任何其他代理设置可能会导致403 Forbidden您使用错误代理的事实几乎没有提示。检查您的gradle.properties或类似的,并根据需要修复/覆盖。

TLDR: define proxies not in the project but on the machine you're working on.

TLDR:不在项目中而是在您正在使用的机器上定义代理。

回答by Manohar Thummanapelly

Due to security violations, organizations may have their own repositories.

由于安全违规,组织可能拥有自己的存储库。

set your local repo as below.

如下设置您的本地仓库。

npm config set registry https://yourorg-artifactory.com/

npm 配置集注册表https://yourorg-artifactory.com/

I hope this will solve the issue.

我希望这能解决这个问题。

回答by tcgumus

On windows 10, do

在 Windows 10 上,执行

npm config edit

This will open config file in a text editor. Delete all the set proxy variables by user and only let default values stay.

这将在文本编辑器中打开配置文件。删除用户设置的所有代理变量,只保留默认值。

;;;;
; npm userconfig file
; this is a simple ini-formatted file
; lines that start with semi-colons are comments.
; read `npm help config` for help on the various options
;;;;

--->Delete everything proxy settings from here.

;;;;
; all options with default values
;;;;

Close and save. Try again. That's what worked for me in my localhost.

关闭并保存。再试一次。这就是在我的本地主机中对我有用的东西。