问题
I am using Node.js 10.0.0 & my index.mjs looks like:
import path from "path";
console.log(__dirname);
In my terminal, I run
node --experimental-modules index.mjs
And I get the following error:
(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)
回答1:
ESM is not node-specific, and node-specific "globals" (such as __dirname and module) will not work. import.meta is expected to provide a suitable replacement.
Source: GitHub issue.
来源:https://stackoverflow.com/questions/50268077/dirname-is-not-defined-in-node-js-10-experimental