78

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

nwinkler
  • 52,665
  • 21
  • 154
  • 168

12 Answers12

157

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.

nwinkler
  • 52,665
  • 21
  • 154
  • 168
  • 2
    Thanks for this answer, but I have this error: "Error: Invalid protocol" – ricardogobbo Aug 30 '12 at 20:01
  • 2
    Worked for me using this answer along with @tedyyu proxy answer (below as I write this). – ixe013 Jan 31 '13 at 14:13
  • 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. – Drellgor Jun 04 '15 at 01:21
  • 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? – Okonomiyaki3000 Jun 30 '16 at 09:27
  • This solution worked for me, until it arrived to some dependencies in which the same error appears despite being using HTTP – David Torres Sep 20 '16 at 10:06
  • Work for me with https not http. – umishra Jun 30 '17 at 09:57
  • Very Nice. Works perfectly. – Vaibhav Jain Jan 17 '18 at 13:24
  • This was the weirdest problem ever. Just started one morning. This answer got me back on track! – Rob Gravelle Oct 12 '18 at 15:32
32
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.

tedyyu
  • 587
  • 6
  • 12
  • 7
    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). – nwinkler Oct 11 '12 at 06:11
  • worked perfectly for me on Windows 7 box on my corporate network – bkwdesign Apr 29 '14 at 16:07
  • As with default: npm config set https-proxy registry.npmjs.org npm config set proxy registry.npmjs.org – jonypera Oct 27 '21 at 13:55
12

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
Olivier C
  • 1,151
  • 10
  • 11
8

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/
atlMapper
  • 764
  • 3
  • 9
  • 24
3

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/
Dharman
  • 30,962
  • 25
  • 85
  • 135
Ali Abbas
  • 1,415
  • 12
  • 19
2

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.

1

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.

Marc.S
  • 31
  • 7
1

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.

tcgumus
  • 328
  • 3
  • 8
1

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

prag
  • 31
  • 1
-1

On windows10, create this file. Worked for me.

enter image description here

jim
  • 228
  • 4
  • 10
-1

my personal access token expired recently. I generated a new one, and replaced the old in .zshrc. Encountered same error as above when ran yarn install.

Authorizing token with SSO fixed it for me. enter image description here

guptaji
  • 161
  • 1
  • 9
-1

In my case, it was failing because I was connected to office VPN. After disconnecting from VPN it was working fine.

Kumar
  • 19
  • 8