由於網路環境限制,直接從 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 的安裝和更新速度應該會有顯著提升!