close

[Solved] ReferenceError: __dirname is not defined in ES module scope

I am facing the following error in my nodejs Project: ReferenceError: __dirname is not defined in ES module scope in Nodejs. We are going to Learn about All Possible Solutions So Lets Get Start with This Article.

How ReferenceError: __dirname is not defined in ES module scope Occurs?

I am facing the following error in my nodejs Project.

ReferenceError: __dirname is not defined in ES module scope

So here I am writing all the possible solutions that I have tried to resolve this error.

How To Solve ReferenceError: __dirname is not defined in ES module scope?

  1. How To Solve ReferenceError: __dirname is not defined in ES module scope?

    To Solve ReferenceError: __dirname is not defined in ES module scope You need to import dirname() method from the path module. So, First of all, You need to import the path module and then use dirname method from the path. Just like this: import path from ‘path’; const __dirname = path.dirname(__filename); dirname will return you just your project path. Just Like This: F:\Nodejs\my_n_project The __dirname variable stores the directory name of the current module.

  2. ReferenceError: __dirname is not defined in ES module scope

    To Solve ReferenceError: __dirname is not defined in ES module scope You need to import dirname() method from the path module. So, First of all, You need to import the path module and then use dirname method from the path. Just like this: import path from ‘path’; const __dirname = path.dirname(__filename); dirname will return you just your project path. Just Like This: F:\Nodejs\my_n_project The __dirname variable stores the directory name of the current module.

Solution 1: use this

You need to import dirname() method from the path module. So, First of all, You need to import the path module and then use dirname method from the path. Just like this.

import path from 'path';

import {fileURLToPath} from 'url';

const __filename = fileURLToPath(import.meta.url);

const __dirname = path.dirname(__filename);
console.log('my_dir_name_is: ', __dirname);

dirname will return you just your project path. Just Like This.

OUTPUT

F:\Nodejs\my_n_project

The __dirname variable stores the directory name of the current module.

Solution 2: an absolute path of the current module

If You need absolute path of the current module then you need to use __filename it will return you an absolute path of the current module.

import path from 'path';
import {fileURLToPath} from 'url';

const __filename = fileURLToPath(import.meta.url);

const __dirname = path.dirname(__filename);
// "F:\Nodejs\my_n_project"

console.log(path.join(__dirname, '/dist', 'index.html'));
// "F:\Nodejs\my_n_project\dist\index.html"

Solution: Import like this

import { fileURLToPath } from 'url';
import { dirname } from 'path';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

Conclusion

It’s all About this error. Hope We solved Your error. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you?

Also, Read

Leave a Comment