Mimicking __dirname and __filename in ESM modules in Node.js

One of the things that I miss most about using ESM is the lack of __dirname and __filename. Here's how to recreate those variables.

CommonJS files running in Node.js have access to two very helpful variables:

  1. __dirname - the directory in which the current file lives.
  2. __filename - the full path to the current file.

In ECMAScript modules, however, these are no longer available by default. Fortunately, you can recreate them yourself to get the same information that to the import.meta.url property:

import { fileURLToPath } from "node:url";
import path from "node:path";

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

The import.meta.url property is a file URL, not a file path, so you first have to convert it into a file path. After that, you just need to use path.dirname() to pull off the directory.

Managing Your Interrupt Rate as a Tech Lead E-book Cover

Take control of your calendar to get more done! The popular blog post series plus frequently asked questions all in one convenient PDF.

Download the Free E-book!

Managing Your Interrupt Rate as a Tech Lead is a free download that arrives in minutes.