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

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

    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.

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

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

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

    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 :)

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

    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.

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

    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

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