You are using Node.js 18.12.1. Vite requires Node.js version 20.19+ or 22.12+.

You are using Node.js 18.12.1. Vite requires Node.js version 20.19+ or 22.12+.

        You are using Node.js 18.12.1. Vite requires Node.js version 20.19+ or 22.12+. Please upgrade your Node.js version.

        当前使用的 Node.js 版本(18.12.1)低于 Vite 要求的最低版本(20.19+ 或 22.12+),导致 Vite 依赖的 Node.js 内置 crypto 模块 API 不兼容(crypto.hash 是高版本 Node.js 才支持的方法)

法一、升级Node版本

        将 Node.js 升级到 20.19.0+22.12.0+ 版本,推荐使用 LTS 版本(长期支持版,如 20.x 最新版)

操作方法(Windows 系统为例):

1. 安装 Node
  • 方法 1:直接下载安装

    访问 Node.js 官网,下载并安装 LTS 版本(v20.x 或 v22.x),安装时会自动覆盖旧版本。

  • 方法 2:使用 nvm 管理版本(推荐,方便切换版本)

    • 先卸载当前已安装的 Node.js(控制面板 -> 程序和功能)。

    • 下载 nvm-windows(选择 nvm-setup.exe)并安装。

    • 打开命令行(管理员模式),执行

# 安装指定版本(例如 20.19.0)
nvm install 20.19.0
# 切换到该版本
nvm use 20.19.0
2. 验证 Node.js 版本

    安装完成后,在命令行中检查版本是否符合要求:

node -v  # 应显示 v20.19.x 或 v22.12.x 以上
3. 重新安装依赖并启动

    由于 Node.js 版本变更,建议清理旧依赖后重新安装:

# 删除 node_modules 和锁文件
rm -rf node_modules package-lock.json  # Windows 可用 rmdir /s/q node_modules 及 del package-lock.json

# 重新安装依赖
npm install

# 启动开发服务器
npm run dev

        完成以上步骤后,Vite 应该能正常启动,crypto.hash 相关的错误会因为 Node.js 版本兼容而消失。

法二、降低vite版本

    如果确实无法升级 Node.js 版本(比如环境限制),可以通过降级 Vite 版本来适配 Node.js 18.12.1(因为高版本 Vite 才强制要求 Node.js 20+,旧版本 Vite 对 Node.js 18 兼容性更好)

    具体方案:降级 Vite 到 4.x 版本(兼容 Node.js 18)

    Vite 4.x 版本的最低 Node.js 要求是 14.18+ / 16+ / 18+,完全兼容你的 Node.js 18.12.1。步骤如下:

1. 卸载当前高版本 Vite 及相关插件

    首先移除项目中不兼容的 Vite 版本(通常是 5.x 及以上),以及可能依赖高版本 Vite 的插件(如 @vitejs/plugin-vue 等,需同步降级):

# 卸载 Vite
npm uninstall vite

# 如果使用了 Vue 插件,也需要卸载(其他框架插件类似)
npm uninstall @vitejs/plugin-vue
2. 安装兼容的 Vite 4.x 版本及对应插件

    安装 Vite 4 的最新稳定版(4.5.3),以及对应版本的插件(确保插件版本与 Vite 4 兼容):

# 安装 Vite 4.5.3(兼容 Node.js 18)
npm install vite@4.5.3 --save-dev

# 安装对应版本的 Vue 插件(如果用 Vue),4.x 版本兼容 Vite 4
npm install @vitejs/plugin-vue@4.6.2 --save-dev

    如果是其他框架(如 React),对应的插件也需要降级(例如 @vitejs/plugin-react 需安装 4.x 版本)。

3. 清理依赖并重新启动
# 清理旧依赖缓存
rm -rf node_modules package-lock.json  # Windows 用:rmdir /s/q node_modules && del package-lock.json

# 重新安装依赖
npm install

# 启动开发服务器
npm run dev

原理说明

Vite 5.x 及以上版本为了使用 Node.js 新特性(如 crypto.hash),将最低 Node.js 版本要求提升到了 20.19+。而 Vite 4.x 版本不依赖这些新 API,因此可以在 Node.js 18 上正常运行。

注意事项

  • 降级后可能无法使用 Vite 5+ 的新功能(如更优的构建性能、新配置项等),但基本开发和构建功能不受影响。
  • 如果项目中还有其他依赖依赖高版本 Vite,可能需要同步降级这些依赖(可通过 npm ls <依赖名> 检查依赖关系)。
转载请说明出处内容投诉
CSS教程网 » You are using Node.js 18.12.1. Vite requires Node.js version 20.19+ or 22.12+.

发表评论

欢迎 访客 发表评论

一个令你着迷的主题!

查看演示 官网购买