报错信息:
[Failed to load PostCSS config: Failed to load PostCSS config (searchPath: D:/project/vite-vue-project): [Error] Must use import to load ES Module: D:\project\vite-vue-project\postcss.config.ts
require() of ES modules is not supported.
require() of D:\project\vite-vue-project\postcss.config.ts from D:\project\vite-vue-project\node_modules\.store\vite@5.3.3\node_modules\vite\dist\node\chunks\dep-CzJTQ5q7.js is an ES module file as it is a .ts file whose nearest parent package.json contains "type": "module" which defines all .ts files in that package scope as ES modules.
Instead change the requiring code to use import(), or remove "type": "module" from D:\project\vite-vue-project\package.json.
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: D:\project\vite-vue-project\postcss.config.ts
require() of ES modules is not supported.
require() of D:\project\vite-vue-project\postcss.config.ts from D:\project\vite-vue-project\node_modules\.store\vite@5.3.3\node_modules\vite\dist\node\chunks\dep-CzJTQ5q7.js is an ES module file as it is a .ts file whose nearest parent package.json contains "type": "module" which defines all .ts files in that package scope as ES modules.
Instead change the requiring code to use import(), or remove "type": "module" from D:\project\vite-vue-project\package.json.
解析:
xxx.js是一个ES模块文件,因为它是一个.ts文件,其最近的父包.json包含“type”:“module”,它将该包范围内的所有.ts文件定义为ES模块。
相反,请更改使用import()所需的代码,或者从D:\project\vite-vue-project\package.json中删除“type”:“module”。
解决方案:
方案1. 找到package.json文件,去掉以下配置
"type": "module",
方案2. 将 postcss.config.js 重命名为 postcss.config.cjs
但vite5有的可能仍然提示cjs已经过时。
方案3. 将 postcss.config.cjs 重命名为 postcss.config.mts 或 postcss.config.cts
参考:Troubleshooting | Vite (vitejs.dev)