【那些年踩过的坑-前端篇- Mac版本】npm init vite 失败,报错`CERT_HAS_EXPIRED npm ERR

CERT_HAS_EXPIRED npm ERR! errno CERT_HAS_EXPIRED npm ERR! request to htt

开发中vue3项目执行npm init vite 命令报错了,vite 需要node.js版本>= 12.0.0

bash 复制代码
beiluo@beiluodeMBP vue3-demo % node -v
v16.13.0
beiluo@beiluodeMBP vue3-demo % npm init vite
npm ERR! code CERT_HAS_EXPIRED
npm ERR! errno CERT_HAS_EXPIRED
npm ERR! request to https://registry.npm.taobao.org/create-vite failed, reason: certificate has expired

npm ERR! A complete log of this run can be found in:`
npm ERR!     /Users/beiluo/.npm/_logs/2024-10-31T15_06_17_487Z-debug.log

CERT_HAS_EXPIRED npm ERR! errno CERT_HAS_EXPIRED npm ERR! request to htt

报错解释:

这个错误通常表示你的计算机无法验证SSL证书的有效性,因为证书已经过期。这可能是因为你的系统时间不正确,或者是因为你的Node.js环境中的证书库过时。

解决方法:(我是使用了清除缓存好了)

1、清除npm缓存

bash 复制代码
npm cache clean --force

2、检查系统时间:

确保你的计算机的日期和时间是正确的。错误的系统时间可能会导致SSL证书验证失败。

3、更新Node.js:

如果你使用的是较旧版本的Node.js,尝试更新到最新稳定版本。

mac更新方法参考我的文章:【那些年踩过的坑-前端篇- Mac版本】Mac电脑如何升级node.js

4、更新操作系统:

确保你的操作系统是最新的,因为它可能包含最新的证书更新。

bash 复制代码
   npm install -g npm@latest

5、 使用环境变量跳过SSL验证(不推荐,仅作为临时解决方案):

bash 复制代码
   npm set strict-ssl=false

mac路径:~/.npmrc

Windows路径:C:\Users.npmrc

这会让npm忽略SSL证书错误,但会降低你的网络安全性。

确保在解决问题后重新启用strict-ssl,或者使用更安全的方式处理证书问题。

6、手动设置registry

尝试手动设置registry为淘宝镜像或者其他镜像源,如下

查看自己的下载源

bash 复制代码
npm config get registry

发现我们之前配置了下载镜像源为https开头的,

bash 复制代码
https://registry.npm.taobao.org/

执行命令清除npm缓存

bash 复制代码
npm cache clean --force

执行命令取消ssl验证

bash 复制代码
npm config set strict-ssl false

再次执行npm install ***,还不行可以尝试设置你的npm镜像源为http开头的,不使用https

bash 复制代码
npm config set registry http://registry.npm.taobao.org

最新的淘宝镜像

bash 复制代码
npm config set registry https://registry.npmmirror.com/
相关推荐
李游Leo1 天前
Node.js 开发环境安装与 npm/pnpm 国内镜像配置(Windows / macOS / Linux)
npm·node.js·pnpm·前端开发·开发环境
晴天166 天前
Node.js 中 `npm install` 命令分析-Day30
前端·npm·node.js
_Rookie._7 天前
Package.json 的imports 和 exports
npm
泠曦れいひ7 天前
在当前项目中安装Vite
前端框架·npm·node.js
Joseph 乔7 天前
【NVM】node 和 nvm 卸载(安装 node & nvm & npm & cnpm & yarn 及配置)
npm·node.js
frjc7 天前
Node.js 与 npm 极简安装教程
前端·npm·node.js
北漂燕郊杨哥10 天前
npm 缓存占满 C 盘?三步把它迁到 D 盘(附验证与避坑)
缓存·npm·node
必须会一定会10 天前
Agent Handoff M5 发布验收:`CHANGES.md`、`npm pack`、`release:check` 与干净环境安装验证
前端·人工智能·npm·node.js·ai编程
剑胆琴心静水深流11 天前
全栈之路6---web集成与呈现
前端·vue.js·spring boot·分布式·spring·前端框架·npm
章鱼小丸子逃跑中11 天前
【2025最新版】如何将fnm与node.js安装在D盘?【保姆级安装及人性话理解教程】
前端·javascript·npm·node.js