NPM 镜像源设置

5 阅读1分钟

在国内使用 npm 时,由于网络原因访问官方源速度较慢,可以通过配置 国内镜像源 来显著提升依赖包的下载速度。

镜像源

常用国内 npm 镜像源

切换镜像源

临时切换(仅当前安装操作)

在安装单个包时,可以临时指定源:

npm install <package-name> --registry=https://registry.npmmirror.com

这种方式不会影响全局配置,适合偶尔需要加速的场景。

永久切换(全局配置)

  • 设置阿里云(npmmirror)镜像

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

  • 验证是否生效

npm config get registry

  • 恢复官方源

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

使用 nrm 快速切换源

  • 安装 nrm 工具

npm install -g nrm

  • 查看可用镜像列表

nrm ls

  • 切换到淘宝镜像(npmmirror)

nrm use taobao

  • 测试各镜像速度

nrm test

建议:日常开发推荐使用 npmmirror,同步速度快且稳定;如需切换多个源,可结合 nrm 管理,方便测试与切换。