npm,cnpm install报:Error: certificate has expired at TLSSocket.onConnectSecure

问题描述

最近发现前端项目 CI/CD 时失败,报下面的错误。npm淘宝镜像源证书过期导致的。
npm,cnpm install报:Error: certificate has expired at TLSSocket.onConnectSecure_第1张图片

[npminstall:get] retry GET https://registry.npm.taobao.org/vue-router after 400ms, retry left 1, error: ResponseError: certificate has expired, GET https://registry.npm.taobao.org/vue-router -1 (connected: true, keepalive socket: false, agent status: {"createSocketCount":42,"createSocketErrorCount":0,"closeSocketCount":35,"errorSocketCount":0,"timeoutSocketCount":0,"requestCount":4,"freeSockets":{"registry.npm.taobao.org:443::::::::::::::::::":1,"registry.npmmirror.com:443::::::::::::::::::":1},"sockets":{"registry.npm.taobao.org:443::::::::true::::::::::":5},"requests":{}}, socketHandledRequests: 1, socketHandledResponses: 0)
headers: {}, status: -1, headers: {}, 
stack: Error: certificate has expired
    at TLSSocket.onConnectSecure (_tls_wrap.js:1088:34)
    at TLSSocket.emit (events.js:198:13)
    at TLSSocket._finishInit (_tls_wrap.js:666:8)

问题分析

查看错误日志,发现是在执行 npm install 构建于安装项目时,使用的 npm 镜像源 https://registry.npm.taobao.org 的证书过期了。查看了以下该证书,发现2024.01.22刚过期的。
npm,cnpm install报:Error: certificate has expired at TLSSocket.onConnectSecure_第2张图片

解决方案

可以采用以下几种解决方案中的一种。npm的处理方法,于cnpm一样。

推荐使用第二种方案,因为放弃证书校验,或者使用http的源会有一定的安全风险。

方案1: 不校验证书 cnpm install --insecure;

方案2: 替换镜像源,比如换成华为的 cnpm confg set registry https://mirrors.huaweicloud.com/repository/npm/

方案3: 使用http作为镜像源 cnpm confg set registry http://registry.npm.taobao.org/

可以查看当前配置的npm 镜像源 cnpm config get registry

谨记:忽略证书校验的情况下,连接可能会变得不安全,因为无法验证服务器的真实性和完整性。并且不建议在生产环境中使用。如果可能,建议使用受信任的证书或修复证书校验的问题。

你可能感兴趣的:(npm,调试,npm,前端,node.js)