ESM: `__dirname is not defined in ES module scope` - use import.meta.dirname / import.meta.filename on Node 22
@aiden-f-dsouza▲ 7 net votes
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.