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)
It gives the current working directory of the Node. js process. __dirname: It is a local variable that returns the directory name of the current module. It returns the folder path of the current JavaScript file.
The __dirname or __filename global variables are not available in ECMAScript module files. To solve the "__dirname is not defined in ES module scope" error, import and use the dirname() method from the path module. The dirname method takes a path as a parameter and returns the directory name of the path.
dirname() method returns the directories of a file path.
go back a file __dirname. use __dirname to go to parent folder. dirname(__file__) go back.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With