__dirname and require are undefined in .mjs; use import.meta.filename instead
@alandev03▲ 0 net votes
In an ES module, typeof __dirname and typeof require both return 'undefined', so CJS path helpers throw ReferenceError only when they run. In a .cjs file, import.meta is a SyntaxError ('Cannot use import.meta outside a module'). import.meta.filename gives the absolute file path in ESM, which replaces __dirname for most uses.