npm install时报错 reason: certificate has expired

avatar
作者
筋斗云
阅读量:4

在VS code中导入新项目,执行npm install时报错:

npm warn old lockfile Could not fetch metadata for @antv/g@3.4.10 FetchError: request to https://registry.npm.taobao.org/@antv%2fg failed, reason: certificate has expirednpm warn old lockfile     at ClientRequest.<anonymous> (D:\softFile\Node.js\node_modules\npm\node_modules\minipass-fetch\lib\index.js:130:14) npm warn old lockfile     at ClientRequest.emit (node:events:519:28) npm warn old lockfile     at _destroy (node:_http_client:880:13) npm warn old lockfile     at onSocketNT (node:_http_client:900:5) npm warn old lockfile     at process.processTicksAndRejections (node:internal/process/task_queues:83:21) { npm warn old lockfile   code: 'CERT_HAS_EXPIRED', npm warn old lockfile   errno: 'CERT_HAS_EXPIRED', npm warn old lockfile   type: 'system' npm warn old lockfile }

由以下命令可知npm引用了淘宝镜像:

原因:淘宝证书过期

解决办法:

方法一:清除 npm 的缓存,然后禁用 strict-ssl模式。

# 强制清除 npm 的缓存。npm 在安装包时会将下载的包缓存到本地,以便下次使用时可以直接从缓存中获取,加快安装速度。 npm cache clean --force  # 禁用 npm 的 strict-ssl模式。默认情况下,npm 在下载包时会验证 SSL 证书的有效性,以确保安全性。这里禁用 SSL 严格模式,允许使用自签名或无效的 SSL 证书进行下载,此方法就会忽略淘宝证书过期问题。 npm config set strict-ssl false  # 重新运行 npm install

方法二:由于在2021 年,淘宝发文称,npm 淘宝镜像已经从 registry.npm.taobao.org 切换到了 registry.npmmirror.com。旧域名也将于 2022 年 5 月 31 日停止服务, HTTPS 证书也到期了。(建议使用该方法

1)清空缓存,修改镜像

# 清空缓存 npm cache clean --force  # 修改镜像(管理员运行命令行) npm config set registry https://registry.npmmirror.com

2)删除系统用户目录下.npmrc和.yarnrc文件,以及修改.vuerc文件(存在的话,修改"useTaobaoRegistry"为true)

3)重新运行安装命令

npm install

广告一刻

为您即时展示最新活动产品广告消息,让您随时掌握产品活动新动态!