全部教程NPM 国内加速配置完全指南

NPM 国内加速配置完全指南

我是 OpenClaw 作者 Quentin。OpenClaw 自己的 CLI 与 agent 模板里满是 npm 包,所以我每天都在和「装包卡死、SSL 出错、443 重置」打交道。这篇是 OpenClaw 团队整理给拼车成员的速查清单——把真踩过的坑写出来。

装 Claude Code 同样要 npm,先把拼车接上比啥都强:

curl -fsSL https://cp.bizq.net/setup.sh | bash -s -- claude-max-20x

快速配置

最快路径是切到淘宝(npmmirror)镜像:

npm config set registry https://registry.npmmirror.com
npm config get registry   # 验证

懒得管细节也可以装 cnpm 让它自带镜像:

npm install -g cnpm --registry=https://registry.npmmirror.com
cnpm install <package-name>

我自己仍然首选直接改 npm 的 registry——少一层包装,bug 也少一层。


可选镜像源

阿里云的淘宝镜像(旧名 npm.taobao.org,现统一到 registry.npmmirror.com)依然是国内同步速度最稳定的一家。其他云厂商可作备份:

镜像源地址维护方
淘宝(npmmirror)https://registry.npmmirror.com阿里云
腾讯云https://mirrors.cloud.tencent.com/npm/腾讯云
华为云https://mirrors.huaweicloud.com/repository/npm/华为云
网易https://mirrors.163.com/npm/网易

经验法则:日常拿淘宝;出问题切腾讯或华为做对照。同一个包在不同镜像同步进度可能差几个小时——遇到 404 先别慌,换镜像很可能就有了。


配置方式:全局 / 项目 / 临时

全局(写到 ~/.npmrc):

npm config list                                            # 看全部
npm config set registry https://registry.npmmirror.com
npm config set registry https://registry.npmjs.org         # 切回官方
npm config delete registry                                 # 删掉自定义

项目级(最稳的做法,提交到 Git):

# .npmrc
registry=https://registry.npmmirror.com

每个 clone 下来的同事自动走同一个源,不用口头交接。

临时(不改全局):

npm install --registry https://registry.npmmirror.com
npm install <package-name> --registry https://registry.npmmirror.com

多镜像切换:nrm

每天要切几次源的人值得装:

npm install -g nrm
 
nrm ls                                     # 列出预置镜像
nrm use taobao
nrm add custom https://my.registry.com
nrm test                                   # ping 各镜像,看谁最快

nrm test 那一下尤其香——别迷信「淘宝最快」,不同地区、不同时段差异挺大。

不想多装一个工具,几行 shell 也行:

#!/bin/bash
case "$1" in
  taobao)   npm config set registry https://registry.npmmirror.com ;;
  official) npm config set registry https://registry.npmjs.org ;;
  tencent)  npm config set registry https://mirrors.cloud.tencent.com/npm/ ;;
  *) echo "用法: $0 {taobao|official|tencent}" ;;
esac

企业内网与代理

公司有自己的 npm 私服(Verdaccio / Nexus / Artifactory)时,配置不止一句 set registry

npm config set registry http://npm.company.com
 
# token 认证(推荐)
npm config set //npm.company.com/:_authToken "your-auth-token"
 
# 老式 base64
npm config set _auth "base64-encoded-credentials"

混合策略:私有包走内网,公共包走镜像:

# .npmrc
registry=https://registry.npmmirror.com
 
@company:registry=http://npm.company.com
//npm.company.com/:_authToken=your-auth-token
 
@babel:registry=https://registry.npmjs.org

走 HTTP 代理:

npm config set proxy       http://proxy.company.com:8080
npm config set https-proxy http://proxy.company.com:8080
npm config set no-proxy    "localhost,127.0.0.1,company.com"
 
npm config delete proxy
npm config delete https-proxy

代理别忘了 https-proxy——只配 proxy 的话 https 请求依然走直连。


pnpm / yarn 也一并配上

维度npmcnpmpnpmyarn
速度最快
磁盘占用最低(硬链接共享)
国内镜像需配置内置需配置需配置
pnpm config set registry https://registry.npmmirror.com
yarn config set registry https://registry.npmmirror.com

Yarn 2/3/4(Berry)走 .yarnrc.yml,配置 key 是 npmRegistryServer 而不是 registry


缓存、超时、SSL

npm cache clean --force                       # 缓存卡了清掉
 
# 国内抖动多,超时拉长能少很多失败
npm config set timeout 60000
npm config set fetch-retries 3
 
# 公司 MITM 代理塞了私 CA:加 CA,别一刀关 SSL
npm config set ca /path/to/ca-cert.pem

npm config set strict-ssl false 是最后手段,生产环境别这么干。


常见问题

1. 镜像设过了还是慢 —— nrm test 看是不是这个镜像本身慢;顺手刷一下 DNS(ipconfig /flushdnssudo dscacheutil -flushcache)。

2. 某个包就是装不下来 —— 镜像同步晚是常因,但「包名笔误 / scope 写错」也很常见,先 npm view <name> 确认。临时切官方源:

npm install <name> --registry https://registry.npmjs.org

3. 公司网络下各种诡异错 —— 通常是 SSL / 防火墙 / 代理三件套:

curl -I https://registry.npmmirror.com   # 测连通性
npm config set proxy       http://proxy:port
npm config set https-proxy http://proxy:port

4. 全局装包权限不足 —— 别 sudo npm i -g,那是把问题埋到下一次。两个干净的解法:

# 把全局目录改成自己的
sudo chown -R $(whoami) $(npm config get prefix)/{lib/node_modules,bin,share}
 
# 或用 nvm 管 Node(更长期)
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash

立即开始

国内装 npm 包的痛苦本质是网络层。镜像、代理、缓存调好之后,剩下的瓶颈就是上游本身。OpenClaw 拼车成员里有不少人就是被 npm install 卡到怀疑人生才装的拼车——好消息是,Claude Code 的安装只需要这一行:

curl -fsSL https://cp.bizq.net/setup.sh | bash -s -- claude-max-20x

完整流程见 cp.bizq.net/setup


相关文章