问题描述:
npm包下载速度慢
问题原因:
为什么下载 npm 包速度慢?
在使用npm下包的时候,默认从国外的https://regitry.npmjs.org/服务器进行下载。此时,网络数据的传输需要经过漫长的海底光缆,因此下包速度会很慢。
解决办法:
方法一:切换 npm 的下载镜像源
bash
// 查看当前的下载镜像源
npm config get registry
// 将下载的镜像源切换为淘宝镜像源
npm config set registry=https://registry.npm.taobao.org/
// 检查镜像源是否下载成功
npm config get registry
或者
bash
// 设置淘宝镜像
npm config set registry https://registry.npmmirror.com
// 验证配置
npm config get registry
恢复默认源
bash
npm config set registry https://registry.npmjs.org
方法二:使用 nrm 管理源
为了更方便的切换下载的镜像源,我们可以安装 nrm 这个小工具,利用 nrm 提供的终端命令,可以快速查看和切换下载的镜像源。
bash
// 通过 npm 包管理器,将 nrm 安装为全局可用的工具
npm i nrm -g
// 查看所有可用的镜像源
nrm ls
// 将下载的镜像源切换为 taobao 镜像
nrm use taobao

