我正在使用 Node.js 10.0.0 & 我的 index.mjs
看起来像:
import path from "path";
console.log(__dirname);
在我的终端中,我运行
node --experimental-modules index.mjs
我收到以下错误:
(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)
最佳答案
ESM 不是特定于 Node 的,特定于 Node 的“全局变量”(例如 __dirname
和模块)将不起作用。 import.meta
有望提供合适的替代品。
来源:GitHub issue .
关于javascript - __dirname 未在 Node.js 10 实验中定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50268077/