__dirname 未在 Node.js 10 实验中定义?

dea*_*904 9 javascript node.js

我正在使用 Node.js 10.0.0 & 我的index.mjs样子:

import path from "path";

console.log(__dirname);
Run Code Online (Sandbox Code Playgroud)

在我的终端中,我运行

node --experimental-modules index.mjs
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

(node:3750) ExperimentalWarning: The ESM module loader is experimental.
ReferenceError: __dirname is not defined
at file:///MyFolderPath/node-10/index.mjs:3:21
at ModuleJob.run (internal/modules/esm/module_job.js:106:14)
Run Code Online (Sandbox Code Playgroud)

J4G*_*NGH 9

ESM 不是特定于节点的,并且特定于节点的“全局变量”(例如__dirname和模块)将不起作用。import.meta有望提供合适的替代品。

来源:GitHub问题