diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js index 332767b3ab225b..35bf80d1253833 100644 --- a/lib/internal/modules/cjs/loader.js +++ b/lib/internal/modules/cjs/loader.js @@ -648,9 +648,11 @@ Module.prototype.load = function(filename) { // Create module entry at load time to snapshot exports correctly const exports = this.exports; if (module !== undefined) { // Called from cjs translator - module.reflect.onReady((reflect) => { - reflect.exports.default.set(exports); - }); + if (module.reflect) { + module.reflect.onReady((reflect) => { + reflect.exports.default.set(exports); + }); + } } else { // preemptively cache ESMLoader.moduleMap.set( url, diff --git a/test/es-module/test-esm-loader-cache-clearing.js b/test/es-module/test-esm-loader-cache-clearing.js new file mode 100644 index 00000000000000..ec22b6a6bb871e --- /dev/null +++ b/test/es-module/test-esm-loader-cache-clearing.js @@ -0,0 +1,11 @@ +// Flags: --experimental-modules +'use strict'; +require('../common'); + +const { cache } = require; + +Object.keys(cache).forEach((key) => { + delete cache[key]; +}); +// Require the same module again triggers the crash +require('../common');