Closed knightedcodemonkey closed 3 months ago
When the dual build is CJS
import.meta.url
require('url').pathToFileURL(__filename).toString()
When the dual build is ESM
__filename
import.meta.filename
fileURLToPath(import.meta.url)
__dirname
import.meta.dirname
dirname(fileURLToPath(import.meta.url))
https://nodejs.org/api/esm.html#no-__filename-or-__dirname
Interesting tsc behavior filed in TypeScript/58658.
tsc
When the dual build is CJS
import.meta.url
with something likerequire('url').pathToFileURL(__filename).toString()
.When the dual build is ESM
__filename
withimport.meta.filename
orfileURLToPath(import.meta.url)
. The latter option allows supporting older node versions.__dirname
withimport.meta.dirname
ordirname(fileURLToPath(import.meta.url))
.https://nodejs.org/api/esm.html#no-__filename-or-__dirname