Building on top of the very good answer by @mark-amery:
A simpler way to maintain compatibility between Node.js versions is to use require
, which supports JSON files:
import { createRequire } from 'node:module';
const require = createRequire(import.meta.url);
const siblingModule = require(configPath);
Node.js also caches what's require
'd, making this version more consistent with the equivalent import
method.
This is supported by all Node.js versions since v12.2.0
and is the solution used by import-from-esm
.