关于代理网络设置
目录
npm
方法一:临时设置(当前终端生效)
在CMD 或 PowerShell 中执行:
CMD:
set HTTP_PROXY=http://代理地址:端口
set HTTPS_PROXY=http://代理地址:端口
set ELECTRON_GET_USE_PROXY=1
npm init electron-app@latest my-app -- --template=vite-typescriptPowerShell:
$env:HTTP_PROXY = "http://代理地址:端口 "
$env:HTTPS_PROXY = "http://代理地址:端口 "
$env:ELECTRON_GET_USE_PROXY = "1"
npm init electron-app@latest my-app -- --template=vite-typescript
ELECTRON_GET_USE_PROXY=1是关键!它告诉 Electron 下载器:“去读代理环境变量!”
方法二:永久设置(推荐)
通过 Windows 系统设置:
1. Win + R → 输入 sysdm.cpl → 回车
2. 高级 → 环境变量
3. 在「用户变量」中新建:
变量名:HTTP_PROXY
变量值:http://代理地址:端口
变量名:HTTPS_PROXY
变量值:http://代理地址:端口
变量名:ELECTRON_GET_USE_PROXY
变量值:1
4. 确定 → 关闭所有终端 → 重新打开终端验证代理是否真的通了
在设置环境变量后,先测试一下:
# 测试 npm 是否通
npm ping
# 测试 Electron 下载地址是否可达
curl -x http://代理地址:端口 https://github.com -I如果 curl 返回 HTTP/2 200,说明代理没问题。
git
# 设置 Git 全局代理(永久生效)
git config --global http.proxy http://代理地址:端口
git config --global https.proxy http://代理地址:端口然后就可以直接:
git clone https://github.com/microsoft/vscode.git验证和管理
# 查看当前 Git 代理配置
git config --global --get http.proxy
git config --global --get https.proxy
# 如果哪天要取消代理
git config --global --unset http.proxy
git config --global --unset https.proxy
# 查看所有 Git 全局配置
git config --global --list进阶:只给 GitHub 设置代理
如果有内网 GitLab 等不需要代理的仓库,可以只对 GitHub 生效:
# 只代理 GitHub(推荐!)
git config --global http.https://github.com.proxy http://代理地址:端口
# 这样:
# git clone https://github.com/xxx → 走代理
# git clone https://你的内网gitlab/xxx → 不走代理
# 取消
git config --global --unset http.https://github.com.proxySSH 方式克隆(git@github.com:xxx)
如果用 SSH 地址克隆,代理配置不同:
编辑文件 C:\Users\mxz\.ssh\config(没有就新建):
Host github.com
HostName github.com
User git
# 通过 HTTP 代理走 SSH
ProxyCommand connect -H 代理地址:端口 %h %p需要安装
connect工具,或者直接用 HTTPS 方式克隆更简单。
总结
| 场景 | 命令 |
|---|---|
| npm | npm config set proxy http://代理地址:端口 |
| Electron 下载 | 环境变量 ELECTRON_GET_USE_PROXY=1 + HTTPS_PROXY |
| Git HTTPS | git config --global http.proxy http://代理地址:端口 |
| Git SSH | 编辑 ~/.ssh/config |