在 CentOS 上配置 Node.js 的代理,通常有 系统级代理、npm 代理、Node 应用代码级代理 几种方式,下面按常见场景说明。
npm config set proxy http://127.0.0.1:7890
npm config set https-proxy http://127.0.0.1:7890
npm config get proxy
npm config get https-proxy
npm config delete proxy
npm config delete https-proxy
yarn config set proxy http://127.0.0.1:7890
yarn config set https-proxy http://127.0.0.1:7890
适用于 fetch、axios、部分 CLI 工具。
export http_proxy=http://127.0.0.1:7890
export https_proxy=http://127.0.0.1:7890
编辑:
vi /etc/profile
添加:
export http_proxy=http://127.0.0.1:7890
export https_proxy=http://127.0.0.1:7890
然后:
source /etc/profile
npm install axios https-proxy-agent
const axios = require('axios');
const { HttpsProxyAgent } = require('https-proxy-agent');
const agent = new HttpsProxyAgent('http://127.0.0.1:7890');
axios.get('https://api.example.com', { httpsAgent: agent })
.then(res => console.log(res.data));
pm2 startup
pm2 env myapp http_proxy http://127.0.0.1:7890
ENV http_proxy=http://host.docker.internal:7890
ENV https_proxy=http://host.docker.internal:7890
npm config set proxy http://user:pass@host:port
npm config set noproxy "registry.npmjs.org,*.example.com"
如果你能说明:
我可以给你更精准的配置方案。