Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

__dirname is not defined in Node.js 10 experimental?

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)
like image 849
deadcoder0904 Avatar asked May 10 '18 07:05

deadcoder0904


People also ask

What is __ Dirname in node js?

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.

Why __ Dirname is not defined?

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.

What is dirname in path?

dirname() method returns the directories of a file path.

How do I go back in dirname?

go back a file __dirname. use __dirname to go to parent folder. dirname(__file__) go back.


1 Answers

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.

like image 92
J4GD33P 51NGH Avatar answered Oct 02 '22 03:10

J4GD33P 51NGH