Skip to content

Commit b79d50b

Browse files
committed
fix: define __dirname manually for ESM compatibility on Node.js versions below 20.11
1 parent f673b64 commit b79d50b

1 file changed

Lines changed: 10 additions & 0 deletions

File tree

index.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
import { createRequire } from "node:module";
2+
import { dirname } from "node:path";
3+
import { fileURLToPath } from "node:url";
24

35
const require = createRequire(import.meta.url);
6+
// `__dirname` is not defined in ES module scope (package.json sets
7+
// "type": "module"), so referencing it crashes the entry on load with
8+
// ERR_AMBIGUOUS_MODULE_SYNTAX on modern Node. `import.meta.dirname` is
9+
// the ESM equivalent on Node ≥ 20.11; fall back to a fileURLToPath-based
10+
// derivation to honor the declared engines floor (node >=18). Resolving
11+
// the native binding path at load time keeps `npm test` and any ESM
12+
// consumer working on plain `import Arkilian from "arkilian"`.
13+
const __dirname = import.meta.dirname ?? dirname(fileURLToPath(import.meta.url));
414
// Runtime resolution via node-gyp-build: picks the prebuilt .node for this
515
// platform/arch from the bundled prebuilds/ dir (offline, no compiler),
616
// falling back to the build/Release output of a source build. Lets

0 commit comments

Comments
 (0)