Skip to content
nkNilay Kabariya

Entry No.002·Fixes··1 min read

Fix: __dirname is not defined in ES module scope (Node 24)

ES modules don't have __dirname or __filename. The one-line modern fix (import.meta.dirname) and the fallback for older Node, both run on Node 24.

by Nilay#node#esm#javascriptFIXED

The error, verbatim

Jump to the fix ↓
console.log(path.join(__dirname, "data.json"));
                      ^
ReferenceError: __dirname is not defined in ES module scope
This file is being treated as an ES module because it has a '.js' file
extension and 'package.json' contains "type": "module".

Tested on

Node
24.14.0
Module type
"type": "module" and .mjs
OS
Windows 11 Pro
Contents
  1. The fix01
  2. Why it happens02
  3. What didn’t work03

The fix

Use import.meta.dirname. It’s built into Node and needs no imports:

import path from "node:path";

const dataFile = path.join(import.meta.dirname, "data.json");
console.log(import.meta.dirname);  // folder of this file
console.log(import.meta.filename); // full path of this file

import.meta.dirname and import.meta.filename were added in Node 20.11. If you’re stuck on an older Node, use the classic two-line fallback:

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

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

Why it happens

__dirname, __filename and require are not real globals. Node injects them into every CommonJS file. ES modules don’t get them, because ESM identifies files by URL (import.meta.url) rather than by path.

Your file is treated as an ES module when either of these is true:

  • package.json has "type": "module" and the file ends in .js
  • the file ends in .mjs

The error message says which one applies to you.

What didn’t work

— N.K., end of entry No.002

Useful? Pass it on:Post on X

Related entries

  1. No.001

    Fix: “Cannot use import statement outside a module”

    The fix for Node and the browser. On Node 24 a plain .js file now often just works, so here's exactly when the error still happens. Reproduced in both.

    > SyntaxError: Cannot use import statement outside a module

    FIXED1 min
  2. No.005

    Fix: ERR_REQUIRE_ESM require() of ES Module not supported

    Why require() of an ES module fails, why it mostly stopped failing on Node 22.12+, and the new errors you hit instead. All reproduced on Node 24.

    > Error [ERR_REQUIRE_ESM]: require() of ES Module ...\node_modules\chalk\source\index.js

    FIXED1 min
  3. No.010

    Fix: Vite “Failed to resolve import” for @ alias imports

    Vite can't find an @/ import until the alias is in vite.config. Why tsconfig paths alone don't help, and the new Vite 8 Rolldown wording. Reproduced.

    > [vite] Internal server error: Failed to resolve import "@/utils/hello"

    FIXED1 min

Post card · Newsletter

Get the next fix in your inbox.

One short email when a new entry is published. No spam, never shared, and you can leave any time.

— Nilay

or follow by RSS

By subscribing you agree to the privacy note. One click to leave.

tip: paste the exact error text