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

前端 未结 29 975
隐瞒了意图╮
隐瞒了意图╮ 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:32

    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

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

    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

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

    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.

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

    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!

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

    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/

    0 讨论(0)
  • 2020-11-22 08:36
    $ npm config set proxy http://login:pass@host:port
    $ npm config set https-proxy http://login:pass@host:port
    
    0 讨论(0)
提交回复
热议问题