代理后面的bower

50

bower install 在代理后失败,出现超时,并伴随以下设置(某些设置是无用的...):

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/

我也尝试过安装/卸载 bower 以及 bower clean cache


1
可能是bower代理配置的重复问题。 - Amir Ali Akbari
1
很好的编译,包含了我们需要做的一切来使一切正常工作!干得好!:+1: - IcyFlame
4个回答

97

编辑你的.bowerrc文件并添加所需的代理配置:

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

如果在经过身份验证的代理后面工作,则应像这样包含用户名和密码:

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

通常,.bowerrc文件与bower.json文件相邻。如果在bower.json文件附近没有.bowerrc文件,则可以自己创建一个。


13
只需在用户目录*~/.bowerrc*中创建.bowerrc文件。 - Stephan Kristyn
9
你也可以在Windows上的C:\Users{用户名}.bowerrc中完成此操作。 - Wayne Ellery
1
@rmic 如何在Windows中查找<主机>和<端口> - Kartheek Sarabu
@Kartheeks 不确定理解你的问题。 <host> 和 <port> 是代理的。 - rmic
2
请注意在 https-proxy 中使用 http://。这对我很有用-如此处所述。 - Felix
显示剩余2条评论

30

我使用bower list命令时遇到了问题,这是因为bower使用git:// URL获取远程GitHub仓库列表,但我们公司的防火墙禁止了git://协议。为了解决这个问题,除了设置环境变量外,我还需要对git进行额外的配置。以下是我需要执行的完整命令列表(记得用你自己的代理主机和端口替换):

# 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://

Bash中的标准环境变量是大写的,对于代理而言它们是HTTP_PROXYHTTPS_PROXY,但有些工具期望这些变量是小写的,Bower就是其中之一。这就是为什么我喜欢设置两种形式的代理:大小写都有。

Bower使用git从GitHub获取包,因此需要将配置键添加到git中。http.proxyhttps.proxy是代理设置,应该指向你的代理。最后但并非最不重要的是,你需要告诉git不要使用git://协议,因为它可能被防火墙拦截。你需要将其替换为标准的http://协议。有人建议像下面这样使用https://代替git://:git config --global url."https://".insteadOf git://,但我遇到了Connection reset by peer错误,所以我使用的是http://,对我而言效果很好。

在家中我不使用任何代理,也没有公司防火墙,因此我更喜欢切换回“正常”的无代理设置。以下是我的做法:

# 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

我不太擅长记忆事物,所以我永远不会记得所有那些命令。除此之外,我很懒,也不想手动输入那些冗长的命令。这就是为什么我创建了函数来设置和取消代理设置。以下是我在一些别名定义后添加到我的.bashrc文件中的两个函数:

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
}

现在当我需要设置代理时,我只需执行set_proxy命令,取消设置则使用unset_proxy命令。借助Bash的自动完成功能,我甚至不需要手动输入这些命令,只需按Tab键即可让系统自动补全。


这对我来说解决了问题。bower searchbower install都运行正常,但是bower list无法工作。需要注意的一件事是,如果您的网络有多个代理服务器,请确保在此处使用与~/.bowerrc中相同的代理,否则您可能会遇到503错误或连接超时! - KingBob

6

我使用Windows上的git bash编写脚本设置代理,但是执行该脚本的用户与我在使用bower时的用户不同。这导致环境变量没有得到考虑。

因此,如其他回答中所指定的,以下设置已足够:

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

0
如果您的操作系统是Linux或者OS X,请尝试下面的命令: bash http_proxy='代理服务器' https_proxy='代理服务器' bower


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接