ReferenceError: __dirname is not defined in ES module scope

运行代码

// $ node -v
// v16.14.0

console.log(__dirname);

报错:

ReferenceError: __dirname is not defined in ES module scope

原因

package.json 加了以下配置

"type": "module",

解决

1、方法一

删除文件 package.json 中的配置项:"type": "module"

2、方法二

import path from "path"

const __dirname = path.resolve();

console.log(__dirname);

参考

猜你喜欢

转载自blog.csdn.net/mouday/article/details/126009755