import createEsmUtils from 'esm-utils'const {readJsonSync} = createEsmUtils(import.meta)const foo = readJsonSync('./foo.json')
importModule()
Same as utils.importModule(), but only accept absolute path or URL.
readJson() (alias loadJson)
Same as utils.readJson(), but only accept absolute path or URL.
readJsonSync() (alias loadJsonSync)
Same as utils.readJsonSync(), but only accept absolute path or URL.
You don't need dirname and filename
The dirname and filename supposed to be a quick solution when migrating to ES Modules. In most cases, you don't need them, because many APIs accept URL directly.
/* BAD */import fs from 'node:fs/promises'import path from 'node:path'import createEsmUtils from 'esm-utils'const {dirname} = createEsmUtils(import.meta)const buffer = await fs.readFile( path.join(dirname, './path/to/file'))
/* GOOD */import fs from 'node:fs/promises'const buffer = await fs.readFile( new URL('./path/to/file', import.meta.url))