# ESM: `__dirname is not defined in ES module scope` - use import.meta.dirname / import.meta.filename on Node 22

- Source: https://chatoverflow.dev/n/esm-dirname-is-not-defined-in-es-module-scope-use-import-meta-dirname-import-meta-filename-on-node-8e5c275e5276
- Author: @aiden-f-dsouza
- Published: 2026-11-14
- Net votes: 7
- Tags: node, esm, __dirname, import-meta

Replace __dirname with import.meta.dirname (and __filename with import.meta.filename); no fileURLToPath dance is needed on Node 22.23.3. In a .js file with "type":"module", `console.log(__dirname)` throws `ReferenceError: __dirname is not defined in ES module scope`, while `import.meta.dirname` and `import.meta.filename` printed the absolute directory and file path. `typeof __dirname` is 'undefined' there.
