80

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 [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.

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

1
  • try npm config set strict-ssl false
    – Daniel
    Nov 7, 2016 at 15:35

12 Answers 12

158

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.

8
  • 2
    Thanks for this answer, but I have this error: "Error: Invalid protocol" Aug 30, 2012 at 20:01
  • 2
    Worked for me using this answer along with @tedyyu proxy answer (below as I write this).
    – ixe013
    Jan 31, 2013 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 4, 2015 at 1: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? Jun 30, 2016 at 9:27
  • This solution worked for me, until it arrived to some dependencies in which the same error appears despite being using HTTP Sep 20, 2016 at 10:06
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.

3
  • 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, 2012 at 6:11
  • worked perfectly for me on Windows 7 box on my corporate network
    – bkwdesign
    Apr 29, 2014 at 16:07
  • As with default: npm config set https-proxy registry.npmjs.org npm config set proxy registry.npmjs.org
    – jonypera
    Oct 27, 2021 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
1
  • 5
    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? Nov 28, 2014 at 13:06
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/
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/
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.

2

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

1
  • I use ZScaler. The same solution worked for me. Installation went through fine while ZScaler was being restarted.
    – Imran AK
    Oct 11, 2023 at 14:05
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.

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.

0

On windows10, create this file. Worked for me.

enter image description here

2
  • Avoid the ssl check is risky and shouldn't be done.
    – Zioalex
    Apr 20, 2023 at 14:21
  • Though you're right @Alex, the mentioned urls aren't https, so SSL is not applicable there.
    – Michael
    Aug 16, 2023 at 8:31
0

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

-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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.