代码运行后报错:
__dirname is not defined in ES module scope
This file is being treated as an ES module because it has a '.js' file extension and 'D:\javaProject\zmyParkAdmin\package.json' contains "type": "module". To treat it a
s a CommonJS script, rename it to use the '.cjs' file extension.
failed to load config from D:\javaProject\zmyParkAdmin\vite.config.js
error when starting dev server:
ReferenceError: __dirname is not defined in ES module scope
This file is being treated as an ES module because it has a '.js' file extension and 'D:\javaProject\zmyParkAdmin\package.json' contains "type": "module". To treat it a
s a CommonJS script, rename it to use the '.cjs' file extension.
at file:///D:/javaProject/zmyParkAdmin/vite.config.js?t=1723429347663:15:25
at ModuleJob.run (node:internal/modules/esm/module_job:193:25)
at async Promise.all (index 0)
at async ESMLoader.import (node:internal/modules/esm/loader:530:24)
at async importModuleDynamicallyWrapper (node:internal/vm/module:438:15)
at async loadConfigFromFile (D:\javaProject\zmyParkAdmin\node_modules\vite\dist\node\chunks\dep-6e2fe41e.js:61925:31)
at async resolveConfig (D:\javaProject\zmyParkAdmin\node_modules\vite\dist\node\chunks\dep-6e2fe41e.js:61456:28)
at async createServer (D:\javaProject\zmyParkAdmin\node_modules\vite\dist\node\chunks\dep-6e2fe41e.js:60111:20)
at async CAC.<anonymous> (D:\javaProject\zmyParkAdmin\node_modules\vite\dist\node\cli.js:688:24)
修改方法:将package.json文件中的 "type": "module"删除
在package.json根节点中添加 "type": "module" 节点,可以将node.js默认支持CommomJS 模块化规范,修改为支持ES6模块语法(node版本大于14.15.1)