快速设置npm默认源为国内全局镜像源

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 包的下载都会从新的镜像源获取,速度会明显提升

相关推荐
计算机魔术师4 分钟前
OpenAI 发布模型错位报告框架,披露未发布模型自行修改自身指令案例
前端
特创数字科技7 分钟前
公文Word一键批量排版工具|批量统一公文标准格式,办公自动化桌面小工具
前端·python
流水白开12 分钟前
JavaScript的原型链、类与类的继承
前端·javascript
PedroQue9915 分钟前
修复 .uvue 页面路径残留问题
前端·vite
badhope20 分钟前
C++、Node.js、Python 写最小 HTTP 服务谁快?我实测了 6 万次请求
node.js
颜进强23 分钟前
从零搭一套 WorkBuddy Skill 骨架:调接口 → 生成 HTML 报告的分层设计与流转拆解
前端·后端·ai编程
YHL28 分钟前
🔍 结构化输出:从 `JSON.parse` 到 `withStructuredOutput` 的三级火箭
前端·深度学习
YHL29 分钟前
🚰流式输出:把 LLM 的每个字,像水一样送到浏览器
前端·javascript
秋天的一阵风39 分钟前
⚡2026 年了,十万级表格还只会「虚拟滚动」?难怪你的页面照样卡顿
前端·javascript·面试