有时候npm连接不上外网导致各种包都安装不了,所以需要设置代理访问。
- 工具1:代理工具
- 工具2:npm包管理工具
- 工具3:http-proxy-to-socks
npm 设置代理的方法
- 假设本地代理端口为9000
npm config set proxy http://127.0.0.1:9000
npm config set https-proxy http://127.0.0.1:9000
- 有用户密码的代理
npm config set proxy http://username:password@127.0.0.1:9000
npm confit set https-proxy http://username:password@127.0.0.1:9000
socks5 代理
npm 不支持 socks 代理,但是我们可以用一个工具将 http 代理转成 socks 代理,然后将 npm 代理地址设置到这个工具的地址。
# 假设本地socks5 代理工具 端口为10808
# 首先安装转换工具
npm install -g http-proxy-to-socks
# 然后使用这个工具监听9000端口,支持http代理,然后所有9000的http代理数据都将转换成socks的代理数据发送到10808上
hpts -s 127.0.0.1:10808 -p 9000
# 最后设置npm代理为9000
npm config set proxy http://127.0.0.1:9000
npm config set https-proxy http://127.0.0.1:9000
相当于又加了一个中间层,将 http 转成 socks。 hpts -s localhost:10808 -p 9000需要一直开着比较麻烦,但是加速效果还是很不错的。