From ef51e923b886a0cd55cb67103bb00cf028082a34 Mon Sep 17 00:00:00 2001 From: ZYSzys Date: Mon, 13 May 2019 17:37:34 +0800 Subject: [PATCH 1/3] esm: fix esm load bug Fixes: https://github.com/nodejs/node/issues/25482 PR-URL: https://github.com/nodejs/node/pull/25491 Reviewed-By: Guy Bedford Reviewed-By: James M Snell Reviewed-By: Rich Trott --- lib/internal/modules/cjs/loader.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js index 7b67efac2390e8..2b137297887979 100644 --- a/lib/internal/modules/cjs/loader.js +++ b/lib/internal/modules/cjs/loader.js @@ -660,10 +660,12 @@ Module.prototype.load = function(filename) { const module = ESMLoader.moduleMap.get(url); // 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 !== undefined) { // Called from cjs translator + if (module.reflect) { + module.reflect.onReady((reflect) => { + reflect.exports.default.set(exports); + }); + } } else { // preemptively cache ESMLoader.moduleMap.set( url, From 6de18496568b1283d7e19484b9ca5dcb7608f4e8 Mon Sep 17 00:00:00 2001 From: Antoine du HAMEL Date: Thu, 2 May 2019 18:57:06 -0400 Subject: [PATCH 2/3] test: clearing require cache crashes esm loader This test shows the regression introduced in v11.4.0: clearing out the require.cache crashes node when using the `--experimental-modules` flag. Refs: https://github.com/nodejs/node/issues/25482 PR-URL: https://github.com/nodejs/node/pull/25491 Reviewed-By: Guy Bedford Reviewed-By: James M Snell Reviewed-By: Rich Trott --- test/es-module/test-esm-loader-cache-clearing.js | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 test/es-module/test-esm-loader-cache-clearing.js 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'); From 77276a4a3708d9709bcad79fc185ad31a1a7e57d Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Sun, 26 May 2019 17:12:57 -0400 Subject: [PATCH 3/3] Fix line change --- lib/internal/modules/cjs/loader.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js index 2b137297887979..44c35718735426 100644 --- a/lib/internal/modules/cjs/loader.js +++ b/lib/internal/modules/cjs/loader.js @@ -660,7 +660,7 @@ Module.prototype.load = function(filename) { const module = ESMLoader.moduleMap.get(url); // create module entry at load time to snapshot exports correctly const exports = this.exports; - if (module !== undefined) { // Called from cjs translator + if (module !== undefined) { // called from cjs translator if (module.reflect) { module.reflect.onReady((reflect) => { reflect.exports.default.set(exports);