国内使用 npm 时配置镜像源

在国内使用 npm 时,由于网络限制可能会遇到下载速度慢或连接超时的问题。通过设置国内镜像源,可以显著提升下载速度和稳定性。以下是常用的国内 npm 镜像源及其配置方法。

查询当前使用的镜像源

npm get registry

设置为淘宝镜像源

npm config set registry https://registry.npmmirror.com/

验证设置

npm get registry

还原为官方源

npm config set registry https://registry.npmjs.org/

国内常用镜像源

npm config set registry https://registry.npmmirror.com/

npm config set registry https://npm.aliyun.com/

npm config set registry https://mirrors.cloud.tencent.com/npm/

npm config set registry https://mirrors.huaweicloud.com/repository/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

参考:npm 最新国内镜像源设置 2025 - 工程师焱记 - 博客园