npm 安装依赖报错 CERT_HAS_EXPIRED 证书过期排障与镜像源更新
执行 npm install 时突然连续报 npm ERR! code CERT_HAS_EXPIRED 与 request to 旧版淘宝镜像源域名 failed。本文详解淘宝旧源证书下线历史与最新 npm 源安全切换实战。
在维护 Node.js 前端或后端项目时,原本一直运行良好的开发环境突然在执行 npm install、yarn 或 npx 时全线崩溃,终端输出大段红色证书失效告警:npm ERR! code CERT_HAS_EXPIRED,随后提示 npm ERR! request to https://registry.npm.taobao.org/axios failed, reason: certificate has expired。
故障现象与报错信息
执行安装命令:
npm install
终端报错输出:
npm ERR! code CERT_HAS_EXPIRED
npm ERR! errno CERT_HAS_EXPIRED
npm ERR! request to https://registry.npm.taobao.org/vue failed, reason: certificate has expired
npm ERR! A complete log can be found in:
npm ERR! /root/.npm/_logs/2026-01-01T10_00_00_123Z-debug-0.log
无论重新安装多少次,即便清空 node_modules 也依然无法下载任何依赖包。
根本原因剖析
- 淘宝 NPM 镜像站历史域名证书正式到期(最核心历史背景):
长期以来,国内前端开发者习惯性将镜像源配置为
https://registry.npm.taobao.org。 阿里巴巴官方镜像团队早已发布公告,旧域名registry.npm.taobao.org已经彻底停用并停止证书续签,全面切换至新域名https://registry.npmmirror.com。旧域名的 HTTPS 证书在到期后直接失效,所有依然指向旧域名的 npm 客户端在 TLS 校验时全部阻断。 - 企业内网私有代理证书失效:使用了公司内部自建的 Nexus / Verdaccio,其配置的 SSL 证书已过有效期。
- 本地开发机系统时间不正确:本地系统时钟发生偏差。
诊断排查步骤
- 查看当前 npm 正在使用的 registry 源地址:
npm config get registry
如果返回的是 https://registry.npm.taobao.org/ 或 http://registry.npm.taobao.org/,100% 确认命中了已下线的废弃老域名。
- 检查当前开发机系统时间:
date
确认年份与月份是否与当前现实时间一致。
解决方案与恢复操作
步骤一:切换至官方最新的官方 npmmirror 镜像源(标准永久修复)
将 npm 的全局注册表源修改为官方最新有效域名:
npm config set registry https://registry.npmmirror.com
如果使用 Yarn:
yarn config set registry https://registry.npmmirror.com
如果使用 pnpm:
pnpm config set registry https://registry.npmmirror.com
步骤二:清理旧缓存并验证
清除可能缓存了旧证书状态的缓存层:
npm cache clean --force
验证源地址与下载恢复:
npm config get registry
# 应返回:https://registry.npmmirror.com
npm install
所有依赖包恢复极速正常下载。
方案三:临时关闭 strict-ssl(仅限内网紧急临时排查)
若连接的是公司内网自建源且短时间内无法更换证书:
npm config set strict-ssl false
警告:
strict-ssl false会彻底放弃对通信证书的合法性核验,切勿在连接公共互联网源时长期开启。修复证书后应及时改回npm config set strict-ssl true。


