const distPath = path.join(__dirname, 'dist', 'index.html');
^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 'xxx\package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.
解决方案:
1. 该文件后缀改为xx.mjs
2. package.json 文件中,添加 "type": "module"
3. 修改一下代码
源代码:
新增代码:
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);