1. 使用 nrm 工具(最推荐)
安装 nrm
npm install -g nrm
查看并测试所有源的速度
# 查看可用源
nrm ls
# 测试各个源响应速度
nrm test
切换到最快的源
# 根据测试结果选择最快的源
nrm use taobao # 淘宝源(国内最快)
# 或
nrm use huawei # 华为云源
# 或
nrm use tencent # 腾讯云源
2. 直接修改 npm 配置
切换到淘宝源(推荐)
npm config set registry https://registry.npmmirror.com/
切换到华为云源
npm config set registry https://mirrors.huaweicloud.com/repository/npm/
切换到腾讯云源
npm config set registry https://mirrors.cloud.tencent.com/npm/
3. 验证源是否修改成功
npm config get registry
4. 国内常用镜像源推荐
| 源名称 | 地址 | 特点 |
|---|---|---|
| 淘宝源 | https://registry.npmmirror.com/ |
国内最流行,同步速度快 |
| 华为云源 | https://mirrors.huaweicloud.com/repository/npm/ |
稳定快速 |
| 腾讯云源 | https://mirrors.cloud.tencent.com/npm/ |
腾讯云用户推荐 |
5. 临时使用特定源(不修改配置)
npm install 包名 --registry=https://registry.npmmirror.com/
6. 恢复官方源
npm config set registry https://registry.npmjs.org/
⭐ 推荐方案
直接使用淘宝源,这是国内最稳定快速的:
npm config set registry https://registry.npmmirror.com/
如果你经常需要切换不同源进行测试,建议安装 nrm 工具,可以很方便地管理和切换源。