最常用、最稳妥的是切换到https://registry.npmmirror.com;执行npm config set registry https://registry.npmmirror.com全局配置,再用npm config get registry验证,输出该地址即成功。

直接改 npm 的 registry 配置就能用国内镜像源加速,最常用、最稳妥的是切换到 https://registry.npmmirror.com(淘宝新镜像,2024 年起全面替代旧域名,证书有效、同步快)。
一、全局配置(推荐,一次设置,所有项目生效)
执行这三条命令即可完成切换和验证:
- 查看当前源:
npm config get registry(若显示https://registry.npmjs.org/,说明还在用官方源) - 设为淘宝新镜像:
npm config set registry https://registry.npmmirror.com - 再次验证:
npm config get registry,输出应为https://registry.npmmirror.com
该方式修改的是用户级配置(通常位于 ~/.npmrc),不影响其他用户,也不需要管理员权限。
二、临时指定源(单次安装,不改配置)
适合快速试装某个包,或在 CI 脚本中避免污染环境:
npm install axios --registry=https://registry.npmmirror.comnpm install -g typescript --registry=https://registry.npmmirror.com
加了 --registry 参数的命令只对本次生效,后续命令仍走默认源。
三、项目级配置(团队协作时统一源)
在项目根目录新建 .npmrc 文件,写入一行:
提交该文件到 Git,所有成员克隆后运行 npm install 就会自动使用该镜像源,无需各自配置。
四、用 nrm 管理多个源(需频繁切换时)
安装并使用 nrm 可视化切换:
- 安装:
npm install -g nrm - 列出可用源:
nrm ls(会显示 npmmirror、tencent、huaweicloud 等) - 切换到淘宝镜像:
nrm use npmmirror - 测试速度(可选):
nrm test
nrm 是命令行工具,适合需要对比或临时切回官方源(如发布包)的场景。
注意:旧地址 registry.npm.taobao.org 已停用,继续使用会报 ERR_CERT_DATE_INVALID。2026 年稳定可用的主流镜像还有腾讯云(https://mirrors.cloud.tencent.com/npm/)、华为云(https://mirrors.huaweicloud.com/repository/npm/),但淘宝新镜像仍是同步最勤(10 分钟一次)、覆盖最全的首选。



















