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.

Understanding JavaScript Promises E-book Cover

Demystify JavaScript promises with the e-book that explains not just concepts, but also real-world uses of promises.

Download the Free E-book!

The community edition of Understanding JavaScript Promises is a free download that arrives in minutes.