由于网络环境限制,直接从 NPM 官方仓库下载包可能会遇到:
配置国内镜像源可以显著提升安装速度和成功率。
推荐使用以下国内镜像源:
镜像源 | URL | 维护方 | 推荐度 |
---|---|---|---|
淘宝镜像 | https://registry.npmmirror.com | 淘宝团队 | ⭐⭐⭐⭐⭐ |
腾讯镜像 | https://mirrors.cloud.tencent.com/npm | 腾讯云 | ⭐⭐⭐⭐ |
华为镜像 | https://mirrors.huaweicloud.com/repository/npm | 华为云 | ⭐⭐⭐⭐ |
中科大镜像 | https://npmreg.proxy.ustclug.org | 中科大 LUG | ⭐⭐⭐ |
# 设置淘宝镜像
npm config set registry https://registry.npmmirror.com
# 验证配置
npm config get registry
# 测试安装速度
npm install -g @anthropic-ai/claude-code
# 设置腾讯镜像
npm config set registry https://mirrors.cloud.tencent.com/npm
# 验证配置
npm config get registry
# 设置华为镜像
npm config set registry https://mirrors.huaweicloud.com/repository/npm
# 验证配置
npm config get registry
在用户主目录或项目根目录创建 .npmrc
文件:
registry=https://registry.npmmirror.com
disturl=https://npmmirror.com/dist
chromedriver_cdnurl=https://npmmirror.com/mirrors/chromedriver
couchbase_binary_host_mirror=https://npmmirror.com/mirrors/couchbase/v{version}
debug_binary_host_mirror=https://npmmirror.com/mirrors/node-inspector
electron_mirror=https://npmmirror.com/mirrors/electron/
flow_binary_host_mirror=https://npmmirror.com/mirrors/flow/v
fse_binary_host_mirror=https://npmmirror.com/mirrors/fsevents
fuse_bindings_binary_host_mirror=https://npmmirror.com/mirrors/fuse-bindings/v{version}
git4win_mirror=https://npmmirror.com/mirrors/git-for-windows
gl_binary_host_mirror=https://npmmirror.com/mirrors/gl/v{version}
grpc_node_binary_host_mirror=https://npmmirror.com/mirrors
hadoop_js_binary_host_mirror=https://npmmirror.com/mirrors/hadoop-js/v{version}
leveldown_binary_host_mirror=https://npmmirror.com/mirrors/leveldown/v{version}
leveldown_hyper_binary_host_mirror=https://npmmirror.com/mirrors/leveldown-hyper/v{version}
mknod_binary_host_mirror=https://npmmirror.com/mirrors/mknod/v{version}
node_sqlite3_binary_host_mirror=https://npmmirror.com/mirrors
nodegit_binary_host_mirror=https://npmmirror.com/mirrors/nodegit/v{version}/
operadriver_cdnurl=https://npmmirror.com/mirrors/operadriver
phantomjs_cdnurl=https://npmmirror.com/mirrors/phantomjs
profiler_binary_host_mirror=https://npmmirror.com/mirrors/node-inspector/
puppeteer_download_host=https://npmmirror.com/mirrors
python_mirror=https://npmmirror.com/mirrors/python
rabin_binary_host_mirror=https://npmmirror.com/mirrors/rabin/v{version}
sass_binary_site=https://npmmirror.com/mirrors/node-sass
sodium_prebuilt_binary_host_mirror=https://npmmirror.com/mirrors/sodium-prebuilt/v{version}
sqlite3_binary_site=https://npmmirror.com/mirrors/sqlite3
utf_8_validate_binary_host_mirror=https://npmmirror.com/mirrors/utf-8-validate/v{version}
uws_binary_host_mirror=https://npmmirror.com/mirrors/uws/v{version}
zmq_prebuilt_binary_host_mirror=https://npmmirror.com/mirrors/zmq-prebuilt/v{version}
registry=https://registry.npmmirror.com
# 临时使用淘宝镜像安装包
npm install --registry=https://registry.npmmirror.com @anthropic-ai/claude-code
# 或使用 npx
npx --registry=https://registry.npmmirror.com @anthropic-ai/claude-code --version
CNPM 是淘宝团队开发的 NPM 客户端,默认使用国内镜像:
# 安装 CNPM
npm install -g cnpm --registry=https://registry.npmmirror.com
# 使用 CNPM 安装包
cnpm install -g @anthropic-ai/claude-code
# 验证版本
cnpm --version
如果你使用 Yarn 包管理器:
# 设置 Yarn 镜像源
yarn config set registry https://registry.npmmirror.com
# 验证配置
yarn config get registry
# 安装 Claude Code
yarn global add @anthropic-ai/claude-code
如果你使用 PNPM 包管理器:
# 设置 PNPM 镜像源
pnpm config set registry https://registry.npmmirror.com
# 验证配置
pnpm config get registry
# 安装 Claude Code
pnpm add -g @anthropic-ai/claude-code
对于企业环境,可能需要配置代理或私有镜像:
# HTTP 代理
npm config set proxy http://proxy.company.com:8080
npm config set https-proxy http://proxy.company.com:8080
# 认证代理
npm config set proxy http://username:[email protected]:8080
npm config set https-proxy http://username:[email protected]:8080
# SOCKS 代理
npm config set proxy socks://proxy.company.com:1080
# 设置私有镜像
npm config set registry http://npm.company.com
# 配置 scoped packages
npm config set @company:registry http://npm.company.com
# 查看当前配置
npm config list
# 查看镜像源
npm config get registry
# 查看完整配置
npm config ls -l | grep registry
# 清理缓存
npm cache clean --force
# 测试安装时间
time npm install -g @anthropic-ai/claude-code
# 查看安装日志
npm install -g @anthropic-ai/claude-code --verbose
症状:
npm ERR! network request to https://registry.npmmirror.com failed
解决方案:
# 切换到其他镜像源
npm config set registry https://mirrors.cloud.tencent.com/npm
# 或恢复官方源
npm config set registry https://registry.npmjs.org
症状:
npm ERR! certificate verify failed
解决方案:
# 临时忽略 SSL 验证(不推荐)
npm config set strict-ssl false
# 或设置证书文件
npm config set ca ""
npm config set cafile /path/to/certificate.pem
症状: 安装失败或速度极慢
解决方案:
# 清除代理配置
npm config delete proxy
npm config delete https-proxy
# 重新设置镜像
npm config set registry https://registry.npmmirror.com
解决方案:
# 清理配置缓存
npm cache clean --force
# 删除 node_modules 重新安装
rm -rf node_modules package-lock.json
npm install
# 检查配置文件位置
npm config get userconfig
npm config get globalconfig
如果需要恢复到官方镜像源:
# 恢复官方镜像
npm config set registry https://registry.npmjs.org
# 删除自定义配置
npm config delete registry
# 或直接编辑配置文件
npm config edit
配置完成后,Claude Code 的安装和更新速度应该会有显著提升!