__filename

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

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

Syntax

__filename  /  import.meta.filename

Returns

string — The absolute file path of the current module.

Examples

// CommonJS
console.log(__filename);
Output
/home/user/app/index.js
// ESM (Node 20.11+)
console.log(import.meta.filename);
console.log(import.meta.url);
Output
/home/user/app/index.js
file:///home/user/app/index.js

Notes

`__filename` is CommonJS only. In ESM use `import.meta.filename` (Node 20.11+) or `fileURLToPath(import.meta.url)`. `import.meta.url` is always available in ESM and is a `file:` URL, not a path.

See also