bower behind a proxy

2019-01-16 04:24发布

问题:

bower install behind a proxy fails in timeout with the following settings (some set are useless...) :

git config --global http.proxy fr-proxy.example.com:3128
git config --global https.proxy fr-proxy.example.com:3128

export http_proxy=http://fr-proxy.example.com:3128
export https_proxy=http://fr-proxy.example.com:3128

npm config set proxy http://fr-proxy.example.com:3128
npm config set https-proxy http://fr-proxy.example.com:3128

npm config set registry http://registry.npmjs.org/

I have also tried an install/uninstall of bower and a bower clean cache.

回答1:

Edit your .bowerrc file and add the wanted proxy configuration:

{
    "proxy":"http://<host>:<port>",
    "https-proxy":"http://<host>:<port>"
}

If working behind an authenticated proxy, user and password should be included like this:

{
    "proxy":"http://<user>:<password>@<host>:<port>",
    "https-proxy":"http://<user>:<password>@<host>:<port>"
}

Usually, the .bowerrc is next to the bower.json. And if there is no .bowerrc file near the bower.json file, you can create one by yourself.



回答2:

I have problem with bower list command, which was caused by the fact that bower use git with git:// URLs to get the list of remote GitHub repositories, but git:// protocol is blocked by our corporate firewall. In order to solve this problem in addition to setting environment variables, I have to add extra configurations to git too. Here's full list of commands I have to execute (remember to replace proxy host and port with yours):

# set proxy for command line tools
export HTTP_PROXY=http://localhost:3128
export HTTPS_PROXY=http://localhost:3128
export http_proxy=http://localhost:3128
export https_proxy=http://localhost:3128

# add configuration to git command line tool
git config --global http.proxy http://localhost:3128
git config --global https.proxy http://localhost:3128
git config --global url."http://".insteadOf git://

Standard environment variables in Bash are uppercased, for proxy those are HTTP_PROXY and HTTPS_PROXY, but some tools expect them to be in lowercase, bower is one of those tools. This is why I prefer to have proxy set in 2 cases: lower and upper.

Bower is using git to get packages from GitHub, this is why configuration keys need to be added to git too. http.proxy and https.proxy are proxy settings and should point to your proxy. Last but not least you need to tell git not to use git:// protocol, because it may be blocked by firewall. You need to replace it with standard http:// protocol. Someones suggest to use https:// instead of git:// like following: git config --global url."https://".insteadOf git://, but I was getting Connection reset by peer error, so I'm using http://, which is working fine for me.

At home I don't use any proxy and don't have corporate firewall, so I prefer to switch back to "normal" no-proxy settings. Here's how I do it:

# remove proxy environment variables
unset HTTP_PROXY
unset HTTPS_PROXY
unset http_proxy
unset https_proxy
# remove git configurations

git config --global --unset http.proxy
git config --global --unset https.proxy
git config --global --unset url."http://".insteadOf

I'm not very good at remembering things, so I would never remember all those commands. On top of this I'm lazy and would not want to type those long commands by hand. This is why I was creating functions to set and unset proxy settings. Here's 2 functions I've added to my .bashrc file after some aliases definitions:

set_proxy() {
    export HTTP_PROXY=http://localhost:3128
    export HTTPS_PROXY=http://localhost:3128
    # some tools uses lowercase env variables
    export http_proxy=http://localhost:3128
    export https_proxy=http://localhost:3128
    # config git
    git config --global http.proxy http://localhost:3128
    git config --global https.proxy http://localhost:3128
    git config --global url."http://".insteadOf git://
}
unset_proxy() {
    unset HTTP_PROXY
    unset HTTPS_PROXY
    unset http_proxy
    unset https_proxy
    git config --global --unset http.proxy
    git config --global --unset https.proxy
    git config --global --unset url."http://".insteadOf
}

Now when I need to set proxy, I just execute set_proxy command, and to unset unset_proxy command. With the help of Bash's autocomplete I don't even need to type those commands, but let tab complete them for me.



回答3:

My script (using git bash on Windows) for setting proxy was executed by a different user from the one I was using for bower. The environment variables were not taken into account.

So the following setting is sufficient, as specified in other answers:

export http_proxy=http://fr-proxy.example.com:3128
export https_proxy=http://fr-proxy.example.com:3128


回答4:

If your OS is Linux or OS X try the following command bash http_proxy='proxy server' https_proxy='proxy server' bower