使用__dirname代替import.meta.url
解决方法示例代码:
// ES Module中 import { fileURLToPath } from 'url'; import { dirname } from 'path';
const __filename = fileURLToPath(import.meta.url); const __dirname = dirname(__filename);
// CommonJS中 const path = require('path'); const __dirname = path.resolve();
注意:在使用CommonJS时,__dirname是当前文件所在的目录,而在ES Module中,__dirname是当前工作目录的路径,因此需要对其进行转换。