1. npm
设置镜像源
# 设置淘宝镜像
npm config set registry https://registry.npmmirror.com
# 或设置腾讯云镜像
npm config set registry https://mirrors.cloud.tencent.com/npm/
恢复官方源
npm config set registry https://registry.npmjs.org/
2. yarn (Classic 版本)
设置镜像源
# 设置淘宝镜像
yarn config set registry https://registry.npmmirror.com
# 或设置腾讯云镜像
yarn config set registry https://mirrors.cloud.tencent.com/npm/
恢复官方源
yarn config set registry https://registry.yarnpkg.com
3. yarn (Berry 版本)
Yarn Berry(2.x+)使用 .yarnrc.yml
文件配置:
npmRegistryServer: "https://registry.npmmirror.com"
4. pnpm
设置镜像源
# 设置淘宝镜像
pnpm config set registry https://registry.npmmirror.com
# 或设置腾讯云镜像
pnpm config set registry https://mirrors.cloud.tencent.com/npm/
恢复官方源
pnpm config set registry https://registry.npmjs.org/
5. cnpm
cnpm
是淘宝定制的 npm 客户端,默认已配置淘宝源。如需自定义:
# 设置镜像源(不推荐,建议直接使用 npm/yarn/pnpm + 镜像)
cnpm config set registry https://registry.npmmirror.com
常用镜像地址
镜像名称 | Registry URL | 备注 |
---|---|---|
淘宝镜像 | https://registry.npmmirror.com | 国内首选,更新频率高 |
腾讯云镜像 | https://mirrors.cloud.tencent.com/npm/ | 腾讯云服务,稳定性强 |
华为云镜像 | https://repo.huaweicloud.com/repository/npm/ | 华为云提供 |
官方源 | https://registry.npmjs.org/ | 国际用户使用 |
私有仓库 | http://your-private-registry.local | 企业内网私有仓库地址 |
额外配置(可选)
设置二进制镜像(适用于 Node.js C++ 模块)
# npm
npm config set disturl https://npmmirror.com/mirrors/node
# yarn
yarn config set disturl https://npmmirror.com/mirrors/node
# pnpm
pnpm config set disturl https://npmmirror.com/mirrors/node
设置 Electron 镜像(加速 Electron 下载)
# npm
npm config set electron_mirror https://npmmirror.com/mirrors/electron/
# yarn
yarn config set electron_mirror https://npmmirror.com/mirrors/electron/
验证配置
查看当前镜像源:
# npm
npm config get registry
# yarn (Classic)
yarn config get registry
# pnpm
pnpm config get registry
注意事项
- 权限问题:全局配置需要管理员权限(Windows 用户以管理员身份运行终端)。
- 项目级配置:可在项目根目录添加
.npmrc
或.yarnrc
文件覆盖全局配置。 - 协议安全:企业私有仓库可能需要
http
协议,建议使用 HTTPS 避免安全问题。