Node.js npm 加速
registry 地址
https://gh-proxy.cn/npm/
结尾的斜杠不要省略。
一键换源脚本
curl -fsSL https://gh-proxy.cn/scripts/npm.sh | sh # 当前用户
sudo sh npm.sh --global # 全局
sh npm.sh --dry-run # 先看改动
sh npm.sh --restore # 还原
curl -fsSL https://gh-proxy.cn/scripts/npm.sh | sh -s -- --dist # 同时指向二进制镜像
脚本只替换 registry 一行,已有的 token 等配置会原样保留。
手动配置
~/.npmrc(pnpm 也读这个文件):
registry=https://gh-proxy.cn/npm/
或者用命令写:
npm config set registry https://gh-proxy.cn/npm/
npm config get registry
yarn:
# yarn 2+:写入 .yarnrc.yml
echo 'npmRegistryServer: "https://gh-proxy.cn/npm/"' >> ~/.yarnrc.yml
# yarn 1.x
yarn config set registry https://gh-proxy.cn/npm/
临时使用
npm install --registry https://gh-proxy.cn/npm/ vue
pnpm add --registry https://gh-proxy.cn/npm/ vue
npx --registry https://gh-proxy.cn/npm/ create-vite
验证
curl -s https://gh-proxy.cn/npm/-/ping
npm view vue version
npm pack vue --dry-run
常见问题
- scoped 包(
@scope/name):无需额外配置,本站按 npm 的原始请求路径转发; - 私有包与发布:
npm publish请继续使用官方或公司 registry,
可以在package.json的publishConfig里单独指定; - electron / node-sass 等二进制下载:用脚本的
--dist参数,
或自行设置ELECTRON_MIRROR等环境变量; - CI 缓存:
npm ci会按 lock 文件里的地址下载,换源后建议重新生成 lock 文件。