333

Read about a proxy variable in a .npmrc file but it does not work. Trying to avoid manually downloading all require packages and installing.

Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324
Ben
  • 4,609
  • 6
  • 21
  • 13

31 Answers31

408

I solved this problem this way:

  1. I run this command:

    npm config set strict-ssl false
    
  2. Then set npm to run with http, instead of https:

    npm config set registry "http://registry.npmjs.org/"
    
  3. Then I install packages using this syntax:

    npm --proxy http://username:password@cacheaddress.com.br:80 install packagename
    

Skip the username:password part if proxy doesn't require you to authenticate

EDIT: A friend of mine just pointed out that you may get NPM to work behind a proxy by setting BOTH HTTP_PROXY and HTTPS_PROXY environment variables, then issuing normally the command npm install express (for example)

EDIT2: As @BStruthers commented, keep in mind that passwords containing "@" wont be parsed correctly, if contains @ put the entire password in quotes

Denilson Sá Maia
  • 47,466
  • 33
  • 109
  • 111
Renato Gama
  • 16,431
  • 12
  • 58
  • 92
  • 7
    Heads up, if your password contains "@" npm won't parse your proxy setting correctly. A potential workaround is to put a bogus username:password in the npm config, and use a local proxy (like fiddler) to modify the request's Proxy-Authorization header to have the correct username:password. Keep in mind that the username:password stored in Proxy-Authorization is base64 encoded. – BStruthers May 21 '13 at 13:18
  • 17
    If your password contains an @ symbol, you can pass it by putting your username and password inside quotes. – absynthe minded web smith Sep 18 '13 at 14:42
  • I set the proxy with the username and password so you don't have to pass it as a parameter every time. Good for a dev environment but wouldn't recommend in production without encryption considerations. – pixelbobby Nov 26 '13 at 15:53
  • 13
    You can have special characters in your password, but they must be url-encoded. So if your password was `my@password`, your .npmrc file should have `my%40password`for the password part. Putting it in quotes works in some cases, but encoding it is foolproof. – Chris Jaynes Dec 12 '14 at 22:29
  • any details about the setting evnironment variables way? thanks – AGamePlayer Sep 08 '15 at 03:34
  • Getting The requested URL /node-static was not found on this server – Ahmed Apr 28 '16 at 17:18
  • 1
    Another gotcha! If you have previous set system variables **HTTP-PROXY** make sure you clear them! – Sydwell Jul 19 '16 at 09:19
  • For domain usernames in Windows it is needed to encode the \ char to `%5C`, so `domain\username` should be `domain%5Cusername` – v.karbovnichy Nov 05 '17 at 10:50
  • @absynthemindedwebsmith `'username':'password'`? something like this? – Pankit Kapadia May 28 '18 at 12:05
  • Yep, although see the following comments for a better way. – absynthe minded web smith May 29 '18 at 12:37
  • How do i find my proxy address? – Robin Sep 24 '18 at 16:43
  • It is still not working. Please see this https://stackoverflow.com/questions/54787536/npm-etimedout-error-while-installing-react – Faizan Mubasher Feb 21 '19 at 06:14
  • This worked seamlessly though need to provide some username:"password" – Suresh Apr 01 '19 at 05:35
  • 1
    You legend! I had given up on trying to get npm to work at work, but this finally resolved it. – tamj0rd2 Apr 12 '19 at 08:36
  • I changed step 2 to use HTTPS protocol and it worked for me `npm config set registry https://registry.npmjs.org/` – Siya Sosibo Jul 18 '19 at 15:10
  • I have set up environment variables: set HTTPS_PROXY=127.0.0.1:8080 set HTTP_PROXY=127.0.0.1:8080 Also, have set proxy values for npm: npm config set proxy 127.0.0.1:8080 npm config set http-proxy 127.0.0.1:8080 npm config set https-proxy 127.0.0.1:8080. With these proxy settings I am not able to visit https pages – Yatin Kanyal Jul 09 '20 at 15:00
  • does this also allow access to `npx` ? – Alex Pappas Dec 17 '20 at 09:41
391

Setup npm proxy

For HTTP:

npm config set proxy http://proxy_host:port

For HTTPS:

use the https proxy address if there is one

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

else reuse the http proxy address

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

Note: The https-proxy doesn't have https as the protocol, but http.

ir2pid
  • 5,604
  • 12
  • 63
  • 107
j.i.t.h.e.s.h
  • 4,128
  • 2
  • 15
  • 7
  • 68
    Note that the https-proxy doesn't have 'https' as the protocol, but 'http'. Changing this solved the problem for me. – peterhil Nov 20 '13 at 11:43
  • 3
    @peterhil Thanks for that tip. It's crazy but I spent hours to resolve this with "https". Any idea why it works like this? – Manoj N V Mar 04 '14 at 06:54
  • 6
    @ManojNV, the connection to the proxy server isn't encrypted. It's not talking HTTPS to the proxy server, just HTTP. The payload is SSL between the client and the destination server. If it were HTTPS to the proxy server, then things would be getting encrypted/decrypted twice. – Jamie Jun 15 '17 at 00:11
  • How do i find my proxy address?\ – Robin Sep 24 '18 at 16:44
  • 1
    Thank you so much for all nice ideas from you guys here! I used command 1, 3 and then run this command `npm config set registry "http://registry.npmjs.org/"`. It worked :) – Travis Le Jun 06 '19 at 09:01
  • I have set up environment variables: set HTTPS_PROXY=127.0.0.1:8080 set HTTP_PROXY=127.0.0.1:8080 Also, have set proxy values for npm: npm config set proxy 127.0.0.1:8080 npm config set http-proxy 127.0.0.1:8080 npm config set https-proxy 127.0.0.1:8080. With these proxy settings I am not able to visit https pages – Yatin Kanyal Jul 09 '20 at 15:01
126

When in doubt, try all these commands, as I do:

npm config set registry http://registry.npmjs.org/
npm config set proxy http://myusername:mypassword@proxy.us.somecompany:8080
npm config set https-proxy http://myusername:mypassword@proxy.us.somecompany:8080
npm config set strict-ssl false
set HTTPS_PROXY=http://myusername:mypassword@proxy.us.somecompany:8080
set HTTP_PROXY=http://myusername:mypassword@proxy.us.somecompany:8080
export HTTPS_PROXY=http://myusername:mypassword@proxy.us.somecompany:8080
export HTTP_PROXY=http://myusername:mypassword@proxy.us.somecompany:8080
export http_proxy=http://myusername:mypassword@proxy.us.somecompany:8080

npm --proxy http://myusername:mypassword@proxy.us.somecompany:8080 \
--without-ssl --insecure -g install

=======

UPDATE

Put your settings into ~/.bashrc or ~/.bash_profile so you don't have to worry about your settings everytime you open a new terminal window!

If your company is like mine, I have to change my password pretty often. So I added the following into my ~/.bashrc or ~/.bash_profile so that whenever I open a terminal, I know my npm is up to date!

  1. Simply paste the following code at the bottom of your ~/.bashrc file:

    ######################
    # User Variables (Edit These!)
    ######################
    username="myusername"
    password="mypassword"
    proxy="mycompany:8080"
    
    ######################
    # Environement Variables
    # (npm does use these variables, and they are vital to lots of applications)
    ######################
    export HTTPS_PROXY="http://$username:$password@$proxy"
    export HTTP_PROXY="http://$username:$password@$proxy"
    export http_proxy="http://$username:$password@$proxy"
    export https_proxy="http://$username:$password@$proxy"
    export all_proxy="http://$username:$password@$proxy"
    export ftp_proxy="http://$username:$password@$proxy"
    export dns_proxy="http://$username:$password@$proxy"
    export rsync_proxy="http://$username:$password@$proxy"
    export no_proxy="127.0.0.10/8, localhost, 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16"
    
    ######################
    # npm Settings
    ######################
    npm config set registry http://registry.npmjs.org/
    npm config set proxy "http://$username:$password@$proxy"
    npm config set https-proxy "http://$username:$password@$proxy"
    npm config set strict-ssl false
    echo "registry=http://registry.npmjs.org/" > ~/.npmrc
    echo "proxy=http://$username:$password@$proxy" >> ~/.npmrc
    echo "strict-ssl=false" >> ~/.npmrc
    echo "http-proxy=http://$username:$password@$proxy" >> ~/.npmrc
    echo "http_proxy=http://$username:$password@$proxy" >> ~/.npmrc
    echo "https_proxy=http://$username:$password@$proxy" >> ~/.npmrc
    echo "https-proxy=http://$username:$password@$proxy" >> ~/.npmrc
    
    ######################
    # WGET SETTINGS
    # (Bonus Settings! Not required for npm to work, but needed for lots of other programs)
    ######################
    echo "https_proxy = http://$username:$password@$proxy/" > ~/.wgetrc
    echo "http_proxy = http://$username:$password@$proxy/" >> ~/.wgetrc
    echo "ftp_proxy = http://$username:$password@$proxy/" >> ~/.wgetrc
    echo "use_proxy = on" >> ~/.wgetrc
    
    ######################
    # CURL SETTINGS
    # (Bonus Settings! Not required for npm to work, but needed for lots of other programs)
    ######################
    echo "proxy=http://$username:$password@$proxy" > ~/.curlrc
    
  2. Then edit the "username", "password", and "proxy" fields in the code you pasted.

  3. Open a new terminal

  4. Check your settings by running npm config list and cat ~/.npmrc

  5. Try to install your module using

    • npm install __, or
    • npm --without-ssl --insecure install __, or
    • override your proxy settings by using npm --without-ssl --insecure --proxy http://username:password@proxy:8080 install __.
    • If you want the module to be available globally, add option -g
Katie
  • 45,622
  • 19
  • 93
  • 125
  • 3
    The last command worked for me. All the ones before that failed – foecum Feb 02 '15 at 08:36
  • 5
    i read about 50 answer concerning this fu***ing proxy configuration...the only that worked was your answer...thank you!!! – Lorenzo Feb 19 '16 at 11:24
  • 4
    Thanks guys!! Glad it's working! This has been a huge headache at work so I'm glad i can help others out :P – Katie Feb 22 '16 at 17:52
  • 2
    +1. This works. I used commands- `npm config set registry http://registry.npmjs.org/`, `npm config set proxy http://myusername:mypassword@proxy.us.somecompany:8080`, `npm config set https-proxy http://myusername:mypassword@proxy.us.somecompany:8080`, `npm config set strict-ssl false` for npm config and then installed npm package using `npm --proxy http://myusername:mypassword@proxy.us.somecompany:8080 --without-ssl --insecure -g install {packagename}`. Thanks – Atur Aug 02 '16 at 11:52
  • Primarily `--without-ssl --insecure` fixed the issue for `shasum` check error I was receiving on installing any package – Atur Aug 02 '16 at 12:04
  • Last one worked for me but without `__`, after i did reset all config to default config values. – rufatZZ Aug 14 '19 at 06:13
  • 1
    After three days of trying this solution worked for me. – Mohan Singh Nov 26 '19 at 05:51
  • @MohanSingh so glad I could help! Sometimes you just gotta throw everything you have at the problem ;) – Katie Nov 26 '19 at 22:35
  • Hello! It is the most detailed answer, but it still didn't work for me. Please, see https://stackoverflow.com/questions/67127855/node-js-error-500-status-code-downloading-tarball – Vadim Volodin Apr 16 '21 at 18:12
  • @Atur (and Katie and readers) you've succesfully installed a malware package now ;) Ok, not sure of that, but downloading a secretly forged package and then failing on a shasum check error might be serious. Please don't suggest that. Its like telling others to disable virusscanners when downloading malware from a phishing site. – Michael May 08 '23 at 14:16
39

Have you tried command-line options instead of the .npmrc file?

I think something like npm --proxy http://proxy-server:8080/ install {package-name} worked for me.

I've also seen the following: npm config set proxy http://proxy-server:8080/

benui
  • 6,440
  • 5
  • 34
  • 49
  • +1 i tried the others this was the one that worked for me. with the auth part from Renato Gama – winner_joiner Jul 09 '13 at 07:05
  • I have set up environment variables: set HTTPS_PROXY=127.0.0.1:8080 set HTTP_PROXY=127.0.0.1:8080 Also, have set proxy values for npm: npm config set proxy 127.0.0.1:8080 npm config set http-proxy 127.0.0.1:8080 npm config set https-proxy 127.0.0.1:8080. With these proxy settings I am not able to visit https pages – Yatin Kanyal Jul 09 '20 at 15:01
23

Though there are already many good advice, for my environment(Windows 7, using PowerShell) and the last version available of node.js ( v8.1.2 ) all the above did not worked, except when I followed brunowego settings.

So check your settings with :

npm config list

Settings behind a proxy:

npm config set registry http://registry.npmjs.org/
npm config set http-proxy http://username:password@ip:port
npm config set https-proxy http://username:password@ip:port
npm config set proxy http://username:password@ip:port
npm set strict-ssl false

Hope this will save time to someone

ThomasReggi
  • 55,053
  • 85
  • 237
  • 424
Carmine Tambascia
  • 1,628
  • 2
  • 18
  • 32
  • how do i find my proxy address? – Robin Sep 24 '18 at 16:44
  • @Robin One way, if using windows, IE stores them. You can go under IE and view LAN settings under connection, and it will show it there. – eaglei22 Sep 27 '18 at 16:23
  • @Robin of course depends on the browser, but usually is under settings – Carmine Tambascia May 11 '19 at 10:01
  • @Robin in the browers under setting you should see / check proxy setting. Usually there is a file with .pac where last like should be : PROXY YourProxyAdress:PORT – Carmine Tambascia Mar 12 '20 at 16:01
  • I have set proxy values for npm: npm config set proxy 127.0.0.1:8080, npm config set http-proxy 127.0.0.1:8080 , npm config set https-proxy 127.0.0.1:8080. With these proxy settings I am not able to visit https pages – Yatin Kanyal Jul 09 '20 at 15:02
  • @Yatin Kanyal because indeed the setting is "http-proxy" try with "https-proxy" as I wrote above. – Carmine Tambascia Feb 07 '21 at 13:46
18

This works for me in Windows:

npm config set proxy http://domain%5Cuser:pass@host:port

If you are not in any domain, use:

npm config set proxy http://user:pass@host:port

If your password contains special characters such as ",@,: and so on, replace them by their URL encoded values. For example "->%22, @->%40, :->%3A. %5C is used for the character \.

maximus
  • 1,290
  • 1
  • 14
  • 18
  • 7
    Thanks for the advice, this worked for me. You can open up your javascript console with ctrl+shift+j and type `encodeURIComponent("YourP@ssword")` to get the encoded version of your password. – jaggedsoft Feb 19 '16 at 23:14
  • I have set proxy values for npm: npm config set proxy 127.0.0.1:8080, npm config set http-proxy 127.0.0.1:8080 , npm config set https-proxy 127.0.0.1:8080. With these proxy settings I am not able to visit https pages – Yatin Kanyal Jul 09 '20 at 15:02
  • Is your proxy on localhost (127.0.0.1)? – Andrei Jul 09 '20 at 15:27
16

To setup the http proxy have the -g flag set:

sudo npm config set proxy http://proxy_host:port -g

For https proxy, again make sure the -g flag is set:

sudo npm config set https-proxy http://proxy_host:port -g

Andrei
  • 7,509
  • 7
  • 32
  • 63
  • what is the meaning of -g? – Dave Aug 11 '15 at 12:55
  • 1
    sets it up globally, not for the local installation – Andrei Aug 11 '15 at 13:02
  • I have set proxy values for npm: npm config set proxy 127.0.0.1:8080, npm config set http-proxy 127.0.0.1:8080 , npm config set https-proxy 127.0.0.1:8080. With these proxy settings I am not able to visit https pages . I tried with -g also – Yatin Kanyal Jul 09 '20 at 15:03
9

This worked for me-

npm config set proxy http://proxy.company.com:8080
npm config set https-proxy http://proxy.company.com:8080
npm set strict-ssl=false
abhishek khandait
  • 1,990
  • 2
  • 15
  • 18
  • I have set proxy values for npm: npm config set proxy 127.0.0.1:8080, npm config set http-proxy 127.0.0.1:8080 , npm config set https-proxy 127.0.0.1:8080. With these proxy settings I am not able to visit https pages – Yatin Kanyal Jul 09 '20 at 15:03
9

Finally i have managed to solve this problem being behinde proxy with AD authentication. I had to execute:

npm config set proxy http://domain%5Cuser:password@proxy:port/
npm config set https-proxy http://domain%5Cuser:password@proxy:port/

It is very important to URL encode any special chars like backshlash or # In my case i had to encode

  1. backshlash with %5C so domain\user will be domain%5Cuser
  2. # sign with %23%0A so password like Password#2 will be Password%23%0A2

I have also added following settings:

npm config set strict-ssl false
npm config set registry http://registry.npmjs.org/
  • I have set proxy values for npm: npm config set proxy 127.0.0.1:8080, npm config set http-proxy 127.0.0.1:8080 , npm config set https-proxy 127.0.0.1:8080. With these proxy settings I am not able to visit https pages – Yatin Kanyal Jul 09 '20 at 15:04
8
$ npm config set proxy http://login:pass@host:port
$ npm config set https-proxy http://login:pass@host:port
Sharan Rajendran
  • 3,549
  • 2
  • 19
  • 6
7

Though i set proxy with config, problem was not solved but after This one worked for me:

npm --https-proxy http://XX.AA.AA.BB:8080 install cordova-plugins

npm --proxy http://XX.AA.AA.BB:8080 install

Community
  • 1
  • 1
Sreedhar GS
  • 2,694
  • 1
  • 24
  • 26
  • This one work for me , no other change . Open command window and use above and it will work – AKS Dec 23 '19 at 18:55
7

vim ~/.npmrc in your Linux machine and add following. Don't forget to add registry part as this cause failure in many cases.

proxy=http://<proxy-url>:<port>
https-proxy=https://<proxy-url>:<port>
registry=http://registry.npmjs.org/
Abhishek Dwivedi
  • 6,557
  • 3
  • 15
  • 20
  • 2
    Many proxies support tunneling https requests, but they will not handle a https connection to themselves. As such, when running into trouble, try modifying `https-proxy=https://..` into `https-proxy=http://..` – YoYo May 16 '18 at 15:42
6

I tried all of these options, but my proxy wasn't having any of it for some reason. Then, born out of desparation/despair, I randomly tried curl in my Git Bash shell, and it worked.

Unsetting all of the proxy options using

npm config rm proxy
npm config rm https-proxy

And then running npm install in my Git Bash shell worked perfectly. I don't know how it's set up correctly for the proxy and the Windows cmd prompt isn't, but it worked.

Evan Knowles
  • 7,426
  • 2
  • 37
  • 71
6

On Windows system

Try removing the proxy and registry settings (if already set) and set environment variables on command line via

SET HTTP_PROXY=http://username:password@domain:port
SET HTTPS_PROXY=http://username:password@domain:port

then try to run npm install. By this, you'll not set the proxy in .npmrc but for that session it will work.

Ken Y-N
  • 14,644
  • 21
  • 71
  • 114
SoSufi
  • 93
  • 1
  • 5
  • 1
    This worked for me. The equals symbol is what seemed to get it all to work. Before that I was just using ```SET HTTP_PROXY http://username:password@domain:port``` but switching to ```SET HTTP_PROXY=http://username:password@domain:port``` seemed to get everything working – Dib Dec 22 '16 at 09:14
6
npm config set proxy <http://...>:<port_number>
npm config set registry http://registry.npmjs.org/

This solved my problem.

venugopal
  • 427
  • 6
  • 13
6

After tying different answers finally, @Kayvar answers's first four lines help me to solve the issue:

npm config set registry http://registry.npmjs.org/
npm config set proxy http://myusername:mypassword@proxy.us.somecompany:8080
npm config set https-proxy http://myusername:mypassword@proxy.us.somecompany:8080
npm config set strict-ssl false
Muhammad Faizan Khan
  • 10,013
  • 18
  • 97
  • 186
5

This worked for me. Set the http and https proxy.

Rohith M
  • 57
  • 1
  • 2
5

For me even though python etc will all work though our corporate proxy npm would not.

I tried

npm config set proxy http://proxyccc.xxx.ca:8080 npm config set https-proxy https://proxyccc.xxx.ca:8080 npm config set registry http://registry.npmjs.org/

as instructed but kept getting the same error.

It was only when I removed https-proxy https://proxyccc.xxx.ca:8080 from the .npmrc file that npm install electron --save-dev worked

Nick Flanagan
  • 51
  • 1
  • 2
  • 1
    Your `https-proxy` is probably not `https:`. At least, having the same port for each is probably not correct, but I think they both probably have the same value. – toddkaufmann Jun 04 '17 at 16:27
5

In my case, I forgot to set the "http://" in my config files (can be found in C: \Users \ [USERNAME] \ .npmrc) proxy adresses. So instead of having

proxy=http://[IPADDRESS]:[PORTNUMBER]
https-proxy=http://[IPADDRESS]:[PORTNUMBER]

I had

proxy=[IPADDRESS]:[PORTNUMBER]
https-proxy=[IPADDRESS]:[PORTNUMBER]

Which of course did not work, but the error messages didnt help much either...

Orsinus
  • 379
  • 5
  • 11
5

There is good information on curl's page on SSL and certificate issues. I base most of my answer on the information there.

Using strict-ssl false is bad practice and can create issues. What we can do instead is add the certificate that is being injected, by the "man in the middle" certificate.

How to solve this on Windows:

  1. Download the CA Certificates from curl based on Mozilla's CA bundle. You can also use curl's "firefox-db2pem.sh" shellscript to convert your local Firefox database.
  2. Go to a webpage using https, for example Stackoverflow in Chrome or Internet Explorer
  3. Click the lock icon, click View certificates or "Valid" in Chrome
  4. Navigate to the Certification path. The top certificate, or the root certificate is the one we want to extract. Click that certificate and then "view certificate"
  5. Click the second tab, "Details". Click "Copy to file". Pick the DER format and make note of where you save the file. Pick a suitable filename, like rootcert.cer
  6. If you have Git installed you will have openssl.exe. Otherwise, install git for windows at this stage. Most likely the openssl executable will be at C:\Program Files\git\usr\bin\openssl.exe. We will use openssl to convert the file to the PEM format we need for NPM to understand it.
  7. Convert the file you saved in step 5 by using this command:
    openssl x509 -inform DES -in **rootcert**.cer -out outcert.pem -text
    where rootcert is the filename of the certificate you saved in step 5.
  8. Open the outcert.pem in a text-editor smart enough to understand line-endings, so not notepad.
  9. Find -----BEGIN CERTIFICATE----- lots of characters -----END CERTIFICATE----- and copy all text between them and also including the BEGIN / END lines
  10. Now we will paste that content to the end of the CA Cert bundle made in step 1. So open the cacert.pem in your advanced texteditor. Go to the end of the file and paste the content from previous step to the end of file. (Preserve the empty line below what you just pasted)
  11. Copy the saved cabundle.pem to a suitable place. Eg your %userprofile% or ~. Make note of the location of the file.
  12. Now we will tell npm/yarn to use the new bundle. In a commandline, write
    npm config set cafile **C:\Users\username\cacert.pem**
    where C:\Users\username\cacert.pem is the path from step 10.
  13. Optionally: turn on strict-ssl again, npm config set strict-ssl true

Phew! We made it! Now npm can understand how to connect. Bonus is that you can tell curl to use the same cabundle.pem and it will also understand HTTPs.

sur
  • 580
  • 8
  • 14
  • via commands: `echo | openssl s_client -showcerts -servername www.domain.com -connect www.domain.com:443 2>/dev/null | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > cacert.crt` to get the `crt` file first; And then generate the pem file via: `openssl x509 -inform PEM -in cacert.crt -out cert.pem -text`; And add pem file into npm cafile via: `npm config set cafile /path/to/cert.perm` – Marslo Apr 26 '22 at 12:59
4

Use below command at cmd or GIT Bash or other prompt

$ npm config set proxy "http://192.168.1.101:4128"

$ npm config set https-proxy "http://192.168.1.101:4128"

where 192.168.1.101 is proxy ip and 4128 is port. change according to your proxy settings. its works for me.

Pankaj
  • 169
  • 2
  • 11
  • 1
    I got to use a domain for authentication, and I used escaped back-slash: with this string %5C . It worked finally! – Francesco Jul 14 '17 at 07:37
4

Try to find .npmrc in C:\Users\.npmrc

then open (notepad), write, and save inside :

proxy=http://<username>:<pass>@<proxyhost>:<port>

PS : remove "<" and ">" please !!

Igor Beaufils
  • 848
  • 2
  • 12
  • 29
4

A lot of applications (e.g. npm) can use proxy setting from user environment variables.

You can just add to your environment following variables HTTP_PROXY and HTTPS_PROXY that will have the same value for each one

http://user:password@proxyAddress:proxyPort

For example if you have Windows you can add proxy as follow:

How it looks on Windows

Dmytro Melnychuk
  • 2,285
  • 21
  • 23
4

There has been many answers above for this question, but none of those worked for me. All of them mentioned to add http:// prefix. So I added it too. All failed.

It finally works after I accidentally removed http:// prefix. Final config is like this:

npm config set registry http://registry.npmjs.org/
npm config set http-proxy ip:port
npm config set https-proxy ip:port
npm config set proxy ip:port
npm set strict-ssl false

I don't know the logic behind this, but it worked. If none of answers above works for you, maybe you can have a try on this way. Hope this one is useful.

Lying_cat
  • 1,288
  • 2
  • 12
  • 16
4

Here are the steps that I've followed (Windows):

  1. Edit the following file C:\Users\<WIN_USERNAME>\.npmrc
  2. Export the certificate to your file system from the following address:https://registry.npmjs.org

  3. Navigate to the exported certificate location and issue the following command:

    npm config set cafile npm_certificate.cer

  4. Add the following changes to the file: registry=https://registry.npmjs.org/ strict-ssl=false https-proxy=http://[proxy_user]:[proxy_password]@[proxy_ip]:[proxy_port]/ cafile=npm_certificate.cer

Now you should be ready to go!

Marco
  • 2,445
  • 2
  • 25
  • 15
4

Just open the new terminal and type npm config edit and npm config -g edit. Reset to defaults. After that close terminal, open the new one and type npm --without-ssl --insecure --proxy http://username:password@proxy:8080 install <package> if you need globally just add -g.

It worked for me, hope it`ll work for you :)

rufatZZ
  • 743
  • 2
  • 11
  • 27
2

when I give without http/http prefix in the proxy settings npm failed even when the proxy host and port were right values. It worked only after adding the protocol prefix.

2

My issue came down to a silly mistake on my part. As I had quickly one day dropped my proxies into a windows *.bat file (http_proxy, https_proxy, and ftp_proxy), I forgot to escape the special characters for the url-encoded domain\user (%5C) and password having the question mark '?' (%3F). That is to say, once you have the encoded command, don't forget to escape the '%' in the bat file command.

I changed

set http_proxy=http://domain%5Cuser:password%3F@myproxy:8080

to

set http_proxy=http://domain%%5Cuser:password%%3F@myproxy:8080

Maybe it's an edge case but hopefully it helps someone.

Brandon Culley
  • 5,219
  • 1
  • 28
  • 28
1

Go TO Environment Variables and Either Remove or set it to empty

HTTP_PROXY and HTTPS_PROXY

it will resolve proxy issue for corporate env too

Codiee
  • 3,047
  • 2
  • 17
  • 18
1

to use proxy in npm install forget all the npm config stuffs and just simply set a http proxy in proxy environment variables and then do the npm i

export https_proxy=http://proxy.address.com:1090/
export http_proxy=http://proxy.address.com:1090/

this always works for me.

im not sure why but npm seems like doesn't works well with socks proxies but it works great with http proxies.

ben
  • 740
  • 7
  • 16
0

I just have have my share of fight with npm and proxy settings and since I do not like other answers I like to share how I think this should be resolved (compromising security is not an option).

What docs says

First of all you have to be aware what are important settings available for npm related to proxy:

  • proxy A proxy to use for outgoing http requests. If the HTTP_PROXY or http_proxy environment variables are set, proxy settings will be honored by the underlying request library.
  • https-proxy A proxy to use for outgoing https requests. If the HTTPS_PROXY or https_proxy or HTTP_PROXY or http_proxy environment variables are set, proxy settings will be honored by the underlying request library.
  • noproxy A comma-separated string or an array of domain extensions that a proxy should not be used for.
  • cafile A path to a file containing one or multiple Certificate Authority signing certificates. Similar to the ca setting, but allows for multiple CA's, as well as for the CA information to be stored in a file on disk.

Now since default values for proxy, https-proxy are based on environment variables it is recommended way to properly configure those variables so other tools could work too (like curl).

Note that for v6 noproxy documentation doesn't say anything about environment variables and since v7 NO_PROXY environment variable is mentioned. My environment wasn't configured to verify how this variable works (if lower case version is covered).

Proper configuration

Now I was configuring docker image which should be used behind a proxy and this entries were needed in Dockerfile:

COPY certs/PoroxyCertificate.crt /usr/local/share/ca-certificates/
COPY certs/RootCa.crt /usr/local/share/ca-certificates/
RUN update-ca-certificates
# here all tools like curl were working

RUN  ["/bin/bash", "-c", "set -o pipefail && curl -sSL https://deb.nodesource.com/setup_14.x  |  bash -"]
RUN apt-get -y update && apt-get install -y nodejs
RUN npm config set cafile /etc/ssl/certs/ca-certificates.crt -g

Now interesting thing is that I needed two certificate files. RootCa.crt is self signed certificate for all corporate servers and PoroxyCertificate.crt contains that certificate, but it also has an extra middle SubCA certificate. Proxy was responding with certificate chain of length 3.

Now update-ca-certificates scans directory /usr/local/share/ca-certificates/ for new certificates and updates /etc/ssl/certs/ca-certificates.crt which will contain much more then those custom certificates.

Feeding this /etc/ssl/certs/ca-certificates.crt to cafile of npm config resolve all problems with certificates when proxy was used.

Important note

with npm v6 certificate errors quite often lead to npm ERR! Maximum call stack size exceeded what is very confusing (I even broke certificate on purpose to verify this issue), log file contained something like this:

RangeError: Maximum call stack size exceeded
    at isDepOptional (/usr/lib/node_modules/npm/lib/install/deps.js:417:24)
    at failedDependency (/usr/lib/node_modules/npm/lib/install/deps.js:441:9)
    at failedDependency (/usr/lib/node_modules/npm/lib/install/deps.js:457:9)
    at failedDependency (/usr/lib/node_modules/npm/lib/install/deps.js:457:9)
    at failedDependency (/usr/lib/node_modules/npm/lib/install/deps.js:457:9)
    at failedDependency (/usr/lib/node_modules/npm/lib/install/deps.js:457:9)
    at failedDependency (/usr/lib/node_modules/npm/lib/install/deps.js:457:9)
    at failedDependency (/usr/lib/node_modules/npm/lib/install/deps.js:457:9)
    at failedDependency (/usr/lib/node_modules/npm/lib/install/deps.js:457:9)
    at failedDependency (/usr/lib/node_modules/npm/lib/install/deps.js:457:9)
    at failedDependency (/usr/lib/node_modules/npm/lib/install/deps.js:457:9)
    at failedDependency (/usr/lib/node_modules/npm/lib/install/deps.js:457:9)
    at failedDependency (/usr/lib/node_modules/npm/lib/install/deps.js:457:9)
    at failedDependency (/usr/lib/node_modules/npm/lib/install/deps.js:457:9)
    at failedDependency (/usr/lib/node_modules/npm/lib/install/deps.js:457:9)
    at failedDependency (/usr/lib/node_modules/npm/lib/install/deps.js:457:9)

I've found some some issue about that, but this will not be fixed in v6.

Marek R
  • 32,568
  • 6
  • 55
  • 140