npm下载东西很容易因为墙壁的原因导致超时,这个有时候把墙砸开了也没用,这里需要你的工具开启tun模式就可以。
如果不会砸墙,那可以配置国内镜像源,下载速度嗖嗖的。
但有个问题需要记住,毕竟是国内镜像源,有一些最新的东西,可能会有差异,导致出错,但一般问题不大
设置全局镜像源
1. 查看当前配置
bash
# 查看当前镜像源
npm config get registry
# 查看所有全局配置
npm config list
2. 设置国内镜像源
淘宝镜像(推荐)
bash
npm config set registry https://registry.npmmirror.com
其他可选镜像源
bash
# 腾讯云
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.ustc.edu.cn/npm/
3. 验证设置
bash
# 查看当前镜像源
npm config get registry
# 预期输出:https://registry.npmmirror.com
# 测试安装一个包(不会真的安装,只是测试)
npm info express
# 或者测试下载速度
npm install -g cnpm --registry=https://registry.npmmirror.com
其他实用命令
bash
# 临时使用镜像源(不修改全局配置)
npm install express --registry=https://registry.npmmirror.com
# 恢复官方源
npm config set registry https://registry.npmjs.org
# 删除镜像源配置(恢复默认)
npm config delete registry
# 使用 nrm 管理多个镜像源(推荐)
npm install -g nrm
nrm ls # 列出所有镜像源
nrm use taobao # 切换到淘宝镜像
nrm test # 测试所有镜像源速度
验证是否生效
运行以下命令检查:
bash
# 1. 查看配置
npm config get registry
# 2. 实际测试下载
npm install lodash --verbose
# 3. 查看网络连接(Mac/Linux)
curl -I https://registry.npmmirror.com
# 4. Windows 用户可以使用
npm install --registry=https://registry.npmmirror.com
设置完成后,所有 npm 包的下载都会从新的镜像源获取,速度会明显提升