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

相关推荐
aichitang20241 小时前
HTML 实时预览工具
前端·html
广州智维科技1 小时前
Kvaser Edge WL400S:工业级边缘计算与 CAN‑FD 数据采集平台解析73-30130-01688-0
前端·edge·边缘计算
吃好睡好便好2 小时前
在Matlab中用sphere( )函数绘制球面图
开发语言·前端·javascript·学习·算法·matlab·信息可视化
黑贝是条狗2 小时前
注册表破解chrome,edge阻止浏览器连接本地websocket
前端·javascript·数据库
UXbot2 小时前
AI 原型工具对比(2026):从文字描述到完整 App 界面的 5 款主流平台评测
android·前端·ios·交互·软件构建
ZC跨境爬虫2 小时前
跟着 MDN 学 HTML day_53:(深入理解 XPathResult 接口)
前端·javascript·ui·html·音视频
摸鱼仙人~2 小时前
html-anything 仓库全面介绍
前端·html
之歆2 小时前
DAY_24JavaScript 面向对象深度全解:Object、构造函数与 this 系统指南(上)
开发语言·前端·javascript·原型模式
梦梦代码精2 小时前
开源智能体平台 BuildingAI 深度解析:Monorepo 架构、MCP 集成及 GPT-Image-2 接入实测
前端·人工智能·后端·gpt·开源·github