快速设置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 分钟前
基于鸿蒙PC Electron框架技术完成的表单验证技术详解
前端·javascript·华为·electron·前端框架·鸿蒙
提子拌饭1335 分钟前
饮料含糖量查询应用 - 鸿蒙PC用Electron框架完整实现
前端·javascript·华为·electron·前端框架·鸿蒙
JustHappy6 分钟前
古法编程秘籍(五):什么是进程和线程?从软件到 CPU 的一次完整旅程
前端·后端·代码规范
爱编程的小金13 分钟前
前端请求库的下一个进化方向:从 Promise 到策略化
前端·alova·前端请求库·请求策略
hsg7714 分钟前
简述:Jensen Huang‘s Footsteps网站全内容分析
前端·javascript·数据库
珑墨39 分钟前
前端 AI 开发通用skill
前端
kyriewen40 分钟前
一个人+Cursor,7天上线付费小程序:第1天我就想放弃了
前端·微信小程序·cursor
大家的林语冰1 小时前
Angular 王者归来,第 22 个主版本亮相,一大波前沿技术再度引领潮流!
前端·javascript·前端框架
老毛肚1 小时前
jeecgboot TS + Vue 模板化 03
前端·javascript·vue.js
下北沢美食家1 小时前
SSE 入门
前端