__dirname

The directory path of the current module (CommonJS), with an ESM equivalent.

Since Node 0.x; import.meta.dirname Node 20.11 Spec ↗

Syntax

__dirname  /  import.meta.dirname

Returns

string — The absolute directory of the current module file.

Examples

// CommonJS
const path = require('node:path');
console.log(path.join(__dirname, 'data.json'));
Output
/home/user/app/data.json
// ESM (Node 20.11+)
import { join } from 'node:path';

console.log(join(import.meta.dirname, 'data.json'));
Output
/home/user/app/data.json

Notes

`__dirname` exists only in CommonJS. In ESM use `import.meta.dirname` (Node 20.11+) or derive it via `path.dirname(fileURLToPath(import.meta.url))` on older versions. Unlike `process.cwd()` it tracks the file, not the launch directory.

See also