为 Git, NPM,Yarn 设置 Proxy

672 阅读1分钟

1. 前言

我有时候会对一些 Proxy 设置进行改动,然后会发现自家的 Git, NPM,Yarn 运行会报类似如下这些错误:

  • unable to access '...'
  • Couldn't resolve host '...'
  • FetchError '...'
  • connect ECONNREFUSED '...'

有时候确实是网络差,但大部分时候都是因为我们使用的 Proxy 设置修改了,而这些应用却没改。

下面我们来看看如何对 Git, NPM,Yarn 进行 Proxy 设置。

注意哦,下面的的端口只是举例,大家需要改为自家的端口。

2. Git

2.1 设置命令

git config --global https.proxy  http://127.0.0.1:1087
git config --global http.proxy  http://127.0.0.1:1087

上述命令是走 HTTP/HTTPS Proxy。

如果你用SS,那么还可以试一下走 socks5,命令如下:

git config --global http.proxy 'socks5://127.0.0.1:1087'
git config --global https.proxy 'socks5://127.0.0.1:1087'

2.2 查看命令

git config --global  --get http.proxy
git config --global  --get https.proxy

如果没有设置过 Proxy,上述命令将什么也不会打印。

2.3 取消命令

git config --global --unset http.proxy
git config --global --unset https.proxy

3. Yarn

3.1 设置命令

yarn config set https-proxy http://127.0.0.1:1087
yarn config set proxy http://127.0.0.1:1087

3.2 查看命令

yarn config list

3.3 取消命令

yarn config delete proxy
yarn config delete https-proxy

4. NPM

4.1 设置命令:

npm config set proxy  http://127.0.0.1:1087
npm config set https-proxy http://127.0.0.1:1087

4.2 查看命令

npm config list

4.2 取消命令:

npm config delete proxy
npm config delete https-proxy

总结

为 Git, NPM,Yarn 设置 Proxy,没啥的,它们各自的文档也有对应说明,这里只是做个汇总,有任何问题欢迎在评论中提出。