Skip to content

Commit ae7789a

Browse files
committed
module: prevent race condition while combining import and require
This checks if any require calls have happened to the same file during the file read. If that was the case, it'll return the same module instead of creating a new instance. PR-URL: #27674 Reviewed-By: Guy Bedford <[email protected]>
1 parent c574b57 commit ae7789a

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

lib/internal/modules/esm/translators.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,16 @@ translators.set('json', async function jsonStrategy(url) {
124124
});
125125
}
126126
const content = await readFileAsync(pathname, 'utf-8');
127+
// A require call could have been called on the same file during loading and
128+
// that resolves synchronously. To make sure we always return the identical
129+
// export, we have to check again if the module already exists or not.
130+
module = CJSModule._cache[modulePath];
131+
if (module && module.loaded) {
132+
const exports = module.exports;
133+
return createDynamicModule(['default'], url, (reflect) => {
134+
reflect.exports.default.set(exports);
135+
});
136+
}
127137
try {
128138
const exports = JsonParse(stripBOM(content));
129139
module = {

0 commit comments

Comments
 (0)