在执行 npm 命令时,默认会从 npm 官方镜像源获取软件包信息。由于官方镜像源位于国外,国内用户在下载和安装软件包时可能会遇到网络延迟问题。

使用以下命令查看当前使用的镜像源:

npm config get registry

默认源地址在国外,从国内访问的速度肯定比较慢

如何修改镜像源

阿里旗下维护着一个完整的 npm 镜像源 npmmirror 镜像站

  1. 临时修改
npm install 软件名 --registry https://registry.npmmirror.com
  1. 全局修改
npm config set registry https://registry.npmmirror.com
  1. 使用第三方软件快速修改、切换 npm 镜像源 nrm — NPM Registry Manager。nrm 不仅可以快速切换镜像源,还可以测试自己网络访问不同源的速度
# 安装 nrm
npm install -g nrm
# 列出当前可用的所有镜像源
> nrm ls
  npm ---------- https://registry.npmjs.org/
  yarn --------- https://registry.yarnpkg.com/
  tencent ------ https://mirrors.tencent.com/npm/
  cnpm --------- https://r.cnpmjs.org/
* taobao ------- https://registry.npmmirror.com/
  npmMirror ---- https://skimdb.npmjs.com/registry/
  huawei ------- https://repo.huaweicloud.com/repository/npm/
# 使用淘宝镜像源
nrm use taobao
# 测试访问速度
nrm test taobao

更多用法请查看 nrm GitHub

Quote