ChatGPT解决这个技术问题 Extra ChatGPT

npm behind a proxy fails with status 403

I'm trying to run npm behind a proxy. I've tried both entering the proxy directly or through 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 info it worked if it ends with ok
npm verb cli [ 'node', '/usr/local/bin/npm', 'search' ]
npm info using npm@1.1.45
npm info using node@v0.8.4
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.

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

try npm config set strict-ssl false

n
nwinkler

OK, so within minutes after posting the question, I found the answer myself here: 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 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.

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.


Thanks for this answer, but I have this error: "Error: Invalid protocol"
Worked for me using this answer along with @tedyyu proxy answer (below as I write this).
I'd like to point out that if you are using 'sudo' with the 'npm' command then you'll also need to use 'sudo' for the 'config set' commands. This took me a bit to figure out, although it seems obvious now.
There's one more wrinkle here that I've recently come across. Some packages will call cache add spec package-name@tarball-url. Presumably, the tarball url comes via npm view which will be https even if you have set your registry to use http. I have not found a solution to this yet. Anyone else?
This solution worked for me, until it arrived to some dependencies in which the same error appears despite being using HTTP
t
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.


As you might have seen, this is the first change that I made - see the first couple of lines in my question. It did not work for me, probably due to the kind of proxy we're running here. I had to additionally change the registry address to use HTTP instead of HTTPS (see my answer).
worked perfectly for me on Windows 7 box on my corporate network
As with default: npm config set https-proxy registry.npmjs.org npm config set proxy registry.npmjs.org
O
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

this seems to be storing the password in plaintext, which is a terrible idea. I'm not sure if it is also transmitted in plaintext via http?
a
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.

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/

D
Dharman

In my case, I read the registry that npm using:

 npm config get registry

and I got

http://registry.npmjs.org/

then I had just changed http to https like this:

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

M
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/

I hope this will solve the issue.


M
Marc.S

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

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 Forbidden with little hint to the fact that you're using the wrong proxy. Check your gradle.properties or such and fix/override as necessary.

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


p
prag

I had the same issue and finally it was resolved by disconnecting from all VPN .


t
tcgumus

On windows 10, do

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.


j
jim

On windows10, create this file. Worked for me.

https://i.stack.imgur.com/XLCRq.png