Is there a way to make npm install (the command) to work behind proxy?

前端 未结 29 973
隐瞒了意图╮
隐瞒了意图╮ 2020-11-22 08:08

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

相关标签:
29条回答
  • 2020-11-22 08:18

    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

    0 讨论(0)
  • 2020-11-22 08:19

    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
    
    0 讨论(0)
  • 2020-11-22 08:20

    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. Select all the text and copy it to clipboard.
    9. 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)
    10. Copy the saved cabundle.pem to a suitable place. Eg your %userprofile% or ~. Make note of the location of the file.
    11. 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.
    12. 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.

    0 讨论(0)
  • 2020-11-22 08:23

    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.

    0 讨论(0)
  • 2020-11-22 08:23

    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 !!

    0 讨论(0)
  • 2020-11-22 08:24

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

    0 讨论(0)
提交回复
热议问题