diff --git a/lib/internal/modules/esm/hooks.js b/lib/internal/modules/esm/hooks.js index d7f8528d5f3b9a..982a5c6dbf3138 100644 --- a/lib/internal/modules/esm/hooks.js +++ b/lib/internal/modules/esm/hooks.js @@ -168,7 +168,7 @@ class Hooks { port: insideLoader, }); - if (preloaded == null) { return; } + if (preloaded == null) { continue; } if (typeof preloaded !== 'string') { // [2] throw new ERR_INVALID_RETURN_VALUE( diff --git a/test/es-module/test-esm-loader-hooks.mjs b/test/es-module/test-esm-loader-hooks.mjs index e36a6f04258d96..97f25f2eaa1969 100644 --- a/test/es-module/test-esm-loader-hooks.mjs +++ b/test/es-module/test-esm-loader-hooks.mjs @@ -348,6 +348,20 @@ describe('Loader hooks', { concurrency: true }, () => { }); }); + it('should handle globalPreload returning undefined', async () => { + const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [ + '--no-warnings', + '--experimental-loader', + 'data:text/javascript,export function globalPreload(){}', + fixtures.path('empty.js'), + ]); + + assert.strictEqual(stderr, ''); + assert.strictEqual(stdout, ''); + assert.strictEqual(code, 0); + assert.strictEqual(signal, null); + }); + it('should be fine to call `process.removeAllListeners("beforeExit")` from the main thread', async () => { const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [ '--no-warnings',