Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

ReferenceError: __dirname is not defined in an ES module

ErrorReferenceError: __dirname is not defined

Short answer

__dirname and __filename are CommonJS variables. When Node.js treats your file as an ES module (.mjs, or type module in package.json), they do not exist. On Node.js 20.11 or 22 use import.meta.dirname and import.meta.filename; on older versions build them from import.meta.url.

  • Applies to Node.js 22, Node.js 24
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Find out whether Node.js treats the file as an ES module. A type of module, or a .mjs extension, means it does.

    node -p "require('./package.json').type"
    node --version
  2. 2Match what you see, then jump to its fix

    1. 1The project has type module in package.json, or the file ends in .mjs, or it uses import and exportThe file is an ES moduleFix
    2. 2No ReferenceError, but path.join(import.meta.dirname, ...) fails because the value is undefinedNode.js is older than 20.11, so import.meta.dirname is undefinedFix
    3. 3The file uses require and __dirname but package.json says type moduleYou want CommonJS but the project is marked as a moduleFix

What does the error look like?

  • ReferenceError: __dirname is not defined
  • ReferenceError: __filename is not defined

Cause 1 of 3

The file is an ES module

1Check

Node.js loads .mjs files as ES modules always, and .js files when the nearest package.json says type module. CommonJS variables are not provided in that mode.

grep -n '"type"' package.json
ls *.mjs 2>/dev/null

2Fix

Replace the variables with import.meta.dirname and import.meta.filename (added in v21.2.0 and v20.11.0).

import path from 'node:path';

const uploads = path.join(import.meta.dirname, 'uploads');
console.log(import.meta.filename);

3Verify

The script runs and prints the absolute path.

node index.js

Confirmed by nodejs.org

Did this fix it?

Cause 2 of 3

Node.js is older than 20.11, so import.meta.dirname is undefined

1Check

The two properties exist only from v20.11.0 and v21.2.0, and only for file: modules.

node --version

2Fix

Derive them from import.meta.url. This works on every Node.js version that supports ES modules.

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

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

3Verify

Print the value once.

node -e "import('node:url').then(u => console.log(u.fileURLToPath('file:///tmp/a.mjs')))"

Confirmed by nodejs.org

Did this fix it?

Cause 3 of 3

You want CommonJS but the project is marked as a module

1Check

Files ending in .cjs are always CommonJS, whatever package.json says. A .js file follows the nearest package.json.

grep -n '"type"' package.json

2Fix

Rename the file to .cjs, or remove the type field if the whole project is CommonJS.

mv server.js server.cjs

3Verify

__dirname works again in that file.

node server.cjs

Confirmed by nodejs.org

Did this fix it?

How do I stop it coming back?

  • Pick one module system per project and set the type field on purpose.
  • Use new URL('./file', import.meta.url) when you only need to read a sibling file.
  • Keep Node.js on a current LTS so the import.meta properties are there.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

ng serve says it cannot determine the project or target

Cannot determine project for command.

The Angular CLI looked through angular.json for a project that defines the target you asked for (serve, build, test) and found none, or found several and could not pick one.

3 causes, each with a fix

Back end

ng serve cannot find the dev-server builder package

Could not find the implementation for builder @angular/build:dev-server

The builder named in angular.json (for example @angular-devkit/build-angular:dev-server) is looked up as an npm package starting from the workspace root.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.