The variables __dirname and __filename are useful variables in Node.js application development.

If your source file is in a folder /Projects/demo-app/index.js and you print the values of these variables, you will get the below output:

$ /Projects/demo-app/index.js      //__filename
$ /Projects/demo-app               //__dirname

But why are these variables useful?

These variables are useful when trying to import other source files or operate in the path where our script is located.

For example, you might have a script where you want to load data from another file that is peer to your script file. See below example

const fs = require('fs')
const path = require('path')

const data = fs.readFileSync(path.join(__dirname, 'helper-data.txt'))

console.log(data.toString())

In the above example, we used the __dirname variable to load the data from the helper-data.txt file. The helper-data.txt file is located in the same folder as the index.js file (the script file).

You might envision other uses of these variables. But that’s not the point of this post.

The point is that __dirname and __filename are not available in the ES module syntax.

For example, if we create a file named index.mjs to activate the ESM syntax and print these variables to the console, we get a ReferenceError.

console.log(__filename)
            ^

ReferenceError: __filename is not defined in ES module scope

SOS – We are missing __dirname and __filename in Node.js ES Module System!

How do we get the __dirname and __filename variables in ES module system?

Is their some alternative?

Yes, there is an alternative.

Check out the below code:

import { dirname } from 'path';

import { fileURLToPath } from 'url';

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

console.log(__filename);
console.log(__dirname);

The ES module system provides a new global constant known as import.meta.url.

If you were to print its value when the script file is stored at the path /Projects/demo-app/index.js, you’ll get the output as file:///Projects/demo-app/index.js. However, Node.js adds the protocol at the beginning (file://, in this case).

To get the pure absolute path, we must use the fileURLToPath function from the url module. We get the directory name by passing the absolute file name to the dirname function from the path module.


The new Node.js __dirname and __filename constants in ES Module Systems work just like CommonJS module system.

If you pass them around, they will contain the value of the original file. Don’t think them as variables that take the role of pointing to the current directory or filename.

The import.meta.url is a shared global variable. Using it makes us more versatile to the requirements of a particular script.

Categories: BlogNodeJS

Saurabh Dashora

Saurabh is a Software Architect with over 12 years of experience. He has worked on large-scale distributed systems across various domains and organizations. He is also a passionate Technical Writer and loves sharing knowledge in the community.

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *