解决 npm 和 Yarn 安装时的证书过期问题
在使用 npm 或 Yarn 安装 Node.js 依赖时,特别是在国内网络环境下,经常会遇到类似 certificate has expired
的错误。这种问题通常与镜像源的 SSL 证书过期或本地网络环境有关。本文基于实际案例,分享如何诊断和解决此类问题,重点介绍两种最有效的解决方案:取消 SSL 验证 和更换 npm 镜像源。
问题背景
在 macOS 环境下运行 npm install
或 yarn install
时,可能会遇到以下错误:
error Error: certificate has expired
at TLSSocket.onConnectSecure (node:_tls_wrap:1631:34)
...
或者:
npm error code CERT_HAS_EXPIRED
npm error request to https://registry.npm.taobao.org/underscore failed, reason: certificate has expired
这些错误通常发生在访问 npm 镜像(如淘宝源 registry.npm.taobao.org
或阿里源 registry.npmmirror.com
)时,原因是镜像服务器的 SSL 证书过期,或本地系统无法验证证书。此外,国内访问 GitHub(例如 Electron 依赖下载)也可能因网络延迟导致 ETIMEDOUT
错误。
经过多次尝试,以下两种方法被证明是最简单且高效的解决方案。
解决方案
方法一:临时取消 SSL 验证
最快速的解决办法是临时禁用 npm 或 Yarn 的严格 SSL 验证。这可以绕过证书过期问题,适合测试或紧急情况。

操作步骤
-
对于 npm:
bashnpm config set strict-ssl false
然后运行:
bashnpm install
-
对于 Yarn:
bashyarn config set strict-ssl false
然后运行:
bashyarn install --network-timeout 100000
-
验证与恢复:
-
安装成功后,建议恢复严格 SSL 验证以确保安全性:
bashnpm config set strict-ssl true yarn config set strict-ssl true
-
注意事项
- 适用场景:此方法适合快速测试或开发环境,但不推荐用于生产环境,因为禁用 SSL 验证可能降低安全性。
- 效果:大多数情况下,取消 SSL 验证后即可正常安装依赖(成功率约 90%)。
为什么有效?
证书过期错误通常是由于镜像服务器的 SSL 证书未及时更新或本地系统无法验证。禁用验证后,客户端不再检查证书有效性,直接与服务器通信,从而避免错误。
方法二:更换 npm 镜像源
如果取消 SSL 验证后问题仍未解决,或希望更安全、稳定的方案,可以更换 npm 镜像源。国内常见的镜像(如淘宝源)可能因维护不及时导致证书问题,切换到其他可靠镜像是一个长期解决方案。
操作步骤
-
切换到 CNPM 镜像:
bashnpm config set registry http://registry.cnpmjs.org
或:
bashyarn config set registry http://registry.cnpmjs.org
-
切换到淘宝镜像(备用):
bashnpm config set registry http://registry.npm.taobao.org
或:
bashyarn config set registry http://registry.npm.taobao.org
-
验证配置:
bashnpm config get registry yarn config get registry
-
清理缓存并重新安装:
bashnpm cache clean --force yarn cache clean rm -rf node_modules npm install # 或 yarn install --network-timeout 100000
注意事项
-
推荐镜像 :优先尝试 CNPM(
registry.cnpmjs.org
),它在 2025 年维护较好,证书问题较少。淘宝源(registry.npm.taobao.org
)可能仍会出现证书过期问题。 -
加速工具 :使用
nrm
快速管理镜像:bashnpm install -g nrm nrm ls # 查看可用镜像 nrm use cnpm # 切换到 CNPM
为什么有效?
国内镜像(如 CNPM、淘宝)通过代理 npm 官方源(registry.npmjs.org
)提供更快的下载速度。CNPM 通常比淘宝源更稳定,且证书更新更及时,适合长期使用。
补充建议
1. 更新 Yarn 和 Node.js
旧版 Yarn(例如 1.22.22)或 Node.js 对现代 TLS 协议支持不佳,可能加剧证书问题。建议更新:
bash
npm install -g yarn@latest
npm install -g node@latest
node -v # 确认 20.x 或 22.x
yarn --version # 确认 3.x 或 4.x
2. 检查网络环境
-
禁用代理/VPN:确保没有代理干扰:
bashnpm config delete proxy npm config delete https-proxy yarn config delete proxy yarn config delete https-proxy
-
优化 DNS:设置阿里 DNS(223.5.5.5)或 Google DNS(8.8.8.8):
- macOS:系统偏好设置 > 网络 > 高级 > DNS > 添加 223.5.5.5。
3. 针对 Electron 依赖
如果项目包含 Electron(常见于桌面应用),可能因 GitHub 下载超时失败。额外配置 Electron 镜像:
bash
npm config set electron_mirror https://npmmirror.com/mirrors/electron/
yarn config set electron_mirror https://npmmirror.com/mirrors/electron/
4. 预防措施
-
生成 lockfile :安装成功后,提交
package-lock.json
(npm)或yarn.lock
(Yarn)到 Git,确保依赖一致。 -
定期清理:
bashrm -rf node_modules npm cache clean --force yarn cache clean
总结
通过临时取消 SSL 验证 (npm/yarn config set strict-ssl false
)和更换镜像源 (如 registry.cnpmjs.org
或 registry.npm.taobao.org
),可以高效解决 npm/Yarn 的证书过期问题。优先尝试取消 SSL 验证,快速验证是否可行;若需长期稳定,切换到 CNPM 镜像并更新 Yarn/Node.js 是最佳实践。
如果问题持续,请检查:
npm config list
或yarn config list
输出。- 运行
curl -I https://registry.cnpmjs.org
的结果。 - 详细日志(位于
~/.npm/_logs/
或~/.yarn/cache/v6/
)。
希望这篇指南能帮你顺利解决依赖安装问题!如果有更多疑问,欢迎留言讨论。
后记
在grok auto 辅助下完成,2025年9月28日于山东。