在国内使用 npm 时,由于网络限制可能会遇到下载速度慢或连接超时的问题。通过设置国内镜像源,可以显著提升下载速度和稳定性。以下是常用的国内 npm 镜像源及其配置方法。
查询当前使用的镜像源
npm get registry
设置为淘宝镜像源
npm config set registry https://registry.npmmirror.com/
验证设置
npm get registry
还原为官方源
npm config set registry https://registry.npmjs.org/
国内常用镜像源
- 淘宝镜像 地址: https://registry.npmmirror.com/ 配置命令:
npm config set registry https://registry.npmmirror.com/
- 阿里云镜像 地址: https://npm.aliyun.com/ 配置命令:
npm config set registry https://npm.aliyun.com/
- 腾讯云镜像 地址: https://mirrors.cloud.tencent.com/npm/ 配置命令:
npm config set registry https://mirrors.cloud.tencent.com/npm/
- 华为云镜像 地址: https://mirrors.huaweicloud.com/repository/npm/ 配置命令:
npm config set registry https://mirrors.huaweicloud.com/repository/npm/
- 清华大学开源镜像站 地址: https://mirrors.tuna.tsinghua.edu.cn/npm/ 配置命令:
npm config set registry https://mirrors.tuna.tsinghua.edu.cn/npm/
查看与验证当前镜像源
- 查看当前使用的镜像源:
npm config get registry
- 验证是否设置成功:
npm info express
快速切换镜像工具:nrm
nrm 是一个 npm 源管理工具,可以快速切换不同的镜像源。
- 安装 nrm:
npm install -g nrm
- 查看可用镜像源:
nrm ls
- 切换到指定镜像源(如淘宝):
nrm use taobao
- 测试各镜像源速度:
nrm test