ReferenceError: __dirname is not defined in an ES module
ReferenceError: __dirname is not definedShort 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.
Fix it fast
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 --version2Match what you see, then jump to its fix
- 1
The 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
No 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
The file uses require and __dirname but package.json says type moduleYou want CommonJS but the project is marked as a moduleFix
- 1
What does the error look like?
ReferenceError: __dirname is not definedReferenceError: __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/null2Fix
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.jsConfirmed by nodejs.org
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 --version2Fix
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
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.json2Fix
Rename the file to .cjs, or remove the type field if the whole project is CommonJS.
mv server.js server.cjs3Verify
__dirname works again in that file.
node server.cjsConfirmed by nodejs.org
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 fixBack end
ng fails with Cannot find module @angular/cli/bin/ng
Cannot find module '@angular/cli/bin/ng'
The ng command is the entry point to the Angular CLI package.
2 causes, each with a fixBack end
Angular app shows 'Cannot GET /' when served by a Node.js server
Cannot GET /dashboard
Cannot GET / is Express saying no route or file matches the request.
3 causes, each with a fixBack 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 fixBack end
Angular build says it cannot resolve '@angular/localize/init'
Can't resolve '@angular/localize/init' in '/usr/src/app/src'
The build imports @angular/localize, and that package is not in node_modules.
2 causes, each with a fixBack end
The command npm run build -- --prod exited with code 1 (Angular)
The command "npm run build -- --prod" exited with code 1
Exit code 1 only says the build failed; the real message is above it.
2 causes, each with a fix