diff --git a/lib/internal/modules/esm/translators.js b/lib/internal/modules/esm/translators.js index 9526e052637725..b1f2c9f2595fc7 100644 --- a/lib/internal/modules/esm/translators.js +++ b/lib/internal/modules/esm/translators.js @@ -123,6 +123,16 @@ translators.set('json', async function jsonStrategy(url) { }); } const content = await readFileAsync(pathname, 'utf-8'); + // A require call could have been called on the same file during loading and + // that resolves synchronously. To make sure we always return the identical + // export, we have to check again if the module already exists or not. + module = CJSModule._cache[modulePath]; + if (module && module.loaded) { + const exports = module.exports; + return createDynamicModule(['default'], url, (reflect) => { + reflect.exports.default.set(exports); + }); + } try { const exports = JsonParse(stripBOM(content)); module = {