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

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

提交回复
热议问题