__dirname is not defined in ES module scope

Posted 嘴巴嘟嘟

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了__dirname is not defined in ES module scope相关的知识,希望对你有一定的参考价值。

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 ‘路径\\package.json’ contains “type”: “module”. To treat it as a CommonJS script, rename it to use
the ‘.cjs’ file extension.

你看报错,上面写清楚了 package.json’ contains “type” 则以.js结尾的文件将被加载为ES模块

import path from "path"
const __dirname = path.resolve();

以上是关于__dirname is not defined in ES module scope的主要内容,如果未能解决你的问题,请参考以下文章

解决运行webpack --config webpack.dev.config.js 报错ReferenceError: _dirname is not defined

webpackJsonp is not defined?

webpack is not defined错误解决

NameError: name ‘_C‘ is not defined

NameError: name ‘_C‘ is not defined

NameError: name ‘_C‘ is not defined