Skip to main content

__filename and __dirname in ESM

ESM does not populate the __filename and __dirname variables frequently used in CommonJS. This post shows what to do instead.

Most robust

This version is supported in all modern Node.js versions. (As of writing this, July 2024, v18-v22.)

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

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

Most succinct

This version is shorter but is still marked as "experimental" in Node.js (here and here) and was only introduced recently into Node.js via v21.2.0 and v20.11.0.

const __filename = import.meta.filename;
const __dirname = import.meta.dirname;