How to Fix ‘__dirname Is Not Defined in ES Module Scope’

The ”_dirname is not defined in ES module scope” error occurs when you use _dirname in an ES module. Here’s how to fix it. 

Written by Ezekiel Oladejo
Published on Jul. 08, 2025
Developer frustrated with error in code
Image: Shutterstock / Built In
Brand Studio Logo
Summary: The “__dirname is not defined in ES module scope” error occurs because __dirname is unavailable in ES modules by default. To fix it, use fileURLToPath(import.meta.url) with Node’s path module to manually define __dirname.

I encountered the ReferenceError: _dirname is not defined in ES module scope while migrating an old project from CommonJS to ES modules. The entry file used __dirname, which is a global variable, and the error message looked like this:

__dirname Error Message
__dirname error message. | Image: Ezekiel Oladejo

This is a common error that indicates you’re trying to use the __dirname variable in an ES module (type: “module” in package.json or using .mjs extension), but __dirname is only available in CommonJS by default.

ReferenceError: “__dirname Is Not defined in ES Module Scope”

  1. Import the path module and fileURLtoPath from the URL module.:
    import path from 'path';
    import { fileURLToPath } from 'url';
    
  2. Replicate the functionality of the CommonJS __dirname variable:
    const __filename = fileURLToPath(import.meta.url); // get the resolved path to the file
    const __dirname = path.dirname(__filename); // get the name of the directory
  3. Verify that the __dirname variable works in your script:
    console.log(__dirname);
     

More on Software EngineeringError: Cannot Find Module in Node Solved

 

What Causes the ‘__dirname Is Not Defined in ES Module Scope’?

By using __dirname in a Node script, you can get the path of the directory housing the current JavaScript file; many Node.js applications use this global variable.

When used in a CommonJS module (when "type": "module" is not defined in your application’s package.json file), __dirname works without hassle, but when used in an ES module, the error __dirname is not defined in ES module scope shows up.

A tutorial on how to resolve the “__dirname is not defined in ES module” error. | Video: Julio Spinelli

More on Sofware EngineeringHow to Use JSON With Comments for Configs

 

How to Resolve ‘__dirname Is Not Defined in ES Module Scope’

First, I reviewed the import.meta object documentation for ES modules. Since what we need is the name of the directory the current script/module is contained in, we can leverage Node.js’ path module. There is also a function in the url module: fileURLToPath, that returns a fully-resolved path to the file.

So, we need to import both the path module and the fileURLToPath function from the url module:

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

In order to replicate the functionality for the CommonJS’ __dirname, we just need to do the following:

const __filename = fileURLToPath(import.meta.url); // get the resolved path to the file
const __dirname = path.dirname(__filename); // get the name of the directory

The code above retrieves the full path of the current ES module file using fileURLToPath(import.meta.url), and then extracts the directory name of that file using path.dirname(), effectively replicating the __dirname behavior in CommonJS for ES modules.

Now, the __dirname variable would work as it was in your script. To verify this in your script, you can just log it right after the definition:

console.log(__dirname);

There it is! I hope this helps you.

Frequently Asked Questions

The ReferenceError: __dirname is not defined in ES module scope occurs because __dirname is a CommonJS-specific global and is not available in ECMAScript modules (ESM) by default.

  1. Import the path module and fileURLtoPath from the URL module.
    import path from 'path';
    import { fileURLToPath } from 'url';
  2. Replicate the functionality of the CommonJS __dirname variable.
    const __filename = fileURLToPath(import.meta.url); // get the resolved path to the file
    const __dirname = path.dirname(__filename); // get the name of the directory
  3. Verify in your script.
    console.log(__dirname);
Explore Job Matches.