From ea08d7f187de068126e319d1e70648def085720f Mon Sep 17 00:00:00 2001 From: Vivek Pandey <98891756+vivek958@users.noreply.github.com> Date: Sat, 25 Oct 2025 20:39:55 +0530 Subject: [PATCH 01/14] Validate source before compiling CJS module. module: fix ERR_INTERNAL_ASSERTION in CJS module loading Fixes: https://github.com/nodejs/node/issues/60401 The loadCJSModule function was not properly validating source content before passing it to compileFunctionForCJSLoader, causing internal assertions when source was null or undefined. This change adds proper source validation and throws a meaningful ERR_INVALID_RETURN_PROPERTY_VALUE error instead of failing with an internal assertion. --- lib/internal/modules/esm/translators.js | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/internal/modules/esm/translators.js b/lib/internal/modules/esm/translators.js index 446349113e13bc..a97d4116c4c3fc 100644 --- a/lib/internal/modules/esm/translators.js +++ b/lib/internal/modules/esm/translators.js @@ -124,8 +124,21 @@ translators.set('module', function moduleStrategy(url, translateContext, parentU * @param {boolean} isMain - Whether the module is the entrypoint */ function loadCJSModule(module, source, url, filename, isMain) { - const compileResult = compileFunctionForCJSLoader(source, filename, false /* is_sea_main */, false); + // Validate source before compilation to prevent internal assertion errors. + // Without this check, null or undefined source causes ERR_INTERNAL_ASSERTION + // when passed to compileFunctionForCJSLoader. + // Refs: https://github.com/nodejs/node/issues/60401 + if (source == null || source === '') { + throw new ERR_INVALID_RETURN_PROPERTY_VALUE( + 'non-empty string', + 'load', + 'source', + source, + ); + } + const compileResult = compileFunctionForCJSLoader(source, filename, false /* is_sea_main */, false); + const { function: compiledWrapper, sourceMapURL, sourceURL } = compileResult; // Cache the source map for the cjs module if present. if (sourceMapURL) { From 8ff9fbe159b673304d1a9a84b6349064b3ecdcbf Mon Sep 17 00:00:00 2001 From: Vivek Pandey <98891756+vivek958@users.noreply.github.com> Date: Sat, 25 Oct 2025 21:10:06 +0530 Subject: [PATCH 02/14] test: add test for null source in ESM loader Add test cases to verify that loadCJSModule properly handles null, undefined, and empty string source values by throwing ERR_INVALID_RETURN_PROPERTY_VALUE instead of triggering ERR_INTERNAL_ASSERTION. Tests three scenarios: - Custom loader returning null source - Custom loader returning undefined source - Custom loader returning empty string source Refs: https://github.com/nodejs/node/issues/60401 --- test/parallel/test-esm-loader-null-source.js | 181 +++++++++++++++++++ 1 file changed, 181 insertions(+) create mode 100644 test/parallel/test-esm-loader-null-source.js diff --git a/test/parallel/test-esm-loader-null-source.js b/test/parallel/test-esm-loader-null-source.js new file mode 100644 index 00000000000000..788e38e5003666 --- /dev/null +++ b/test/parallel/test-esm-loader-null-source.js @@ -0,0 +1,181 @@ +'use strict'; + +// Test that ESM loader handles null/undefined source gracefully +// and throws meaningful error instead of ERR_INTERNAL_ASSERTION. +// Refs: https://github.com/nodejs/node/issues/60401 + +const common = require('../common'); +const assert = require('assert'); +const { spawnSync } = require('child_process'); +const fixtures = require('../common/fixtures'); + +// Test case: Loader returning null source for CommonJS module +// This should throw ERR_INVALID_RETURN_PROPERTY_VALUE, not ERR_INTERNAL_ASSERTION +{ + const result = spawnSync( + process.execPath, + [ + '--no-warnings', + '--input-type=module', + '--eval', + ` + import { register } from 'node:module'; + + // Register a custom loader that returns null source + const code = 'export function load(url, context, next) {' + + ' if (url.includes("test-null-source")) {' + + ' return { format: "commonjs", source: null, shortCircuit: true };' + + ' }' + + ' return next(url);' + + '}'; + + register('data:text/javascript,' + encodeURIComponent(code)); + + try { + await import('file:///test-null-source.js'); + console.log('ERROR: Should have thrown'); + process.exit(1); + } catch (err) { + // Should throw ERR_INVALID_RETURN_PROPERTY_VALUE, not ERR_INTERNAL_ASSERTION + if (err.code === 'ERR_INTERNAL_ASSERTION') { + console.log('FAIL: Got ERR_INTERNAL_ASSERTION'); + process.exit(1); + } + if (err.code === 'ERR_INVALID_RETURN_PROPERTY_VALUE') { + console.log('PASS: Got expected error'); + process.exit(0); + } + console.log('ERROR: Got unexpected error:', err.code); + process.exit(1); + } + `, + ], + { encoding: 'utf8' } + ); + + const output = result.stdout + result.stderr; + + // Verify test passed + assert.ok( + output.includes('PASS: Got expected error'), + 'Should pass with expected error. Output: ' + output + ); + + assert.strictEqual( + result.status, + 0, + 'Process should exit with code 0. Output: ' + output + ); +} + +// Test case: Loader returning undefined source +{ + const result = spawnSync( + process.execPath, + [ + '--no-warnings', + '--input-type=module', + '--eval', + ` + import { register } from 'node:module'; + + const code = 'export function load(url, context, next) {' + + ' if (url.includes("test-undefined-source")) {' + + ' return { format: "commonjs", source: undefined, shortCircuit: true };' + + ' }' + + ' return next(url);' + + '}'; + + register('data:text/javascript,' + encodeURIComponent(code)); + + try { + await import('file:///test-undefined-source.js'); + console.log('ERROR: Should have thrown'); + process.exit(1); + } catch (err) { + if (err.code === 'ERR_INTERNAL_ASSERTION') { + console.log('FAIL: Got ERR_INTERNAL_ASSERTION'); + process.exit(1); + } + if (err.code === 'ERR_INVALID_RETURN_PROPERTY_VALUE') { + console.log('PASS: Got expected error'); + process.exit(0); + } + console.log('ERROR: Got unexpected error:', err.code); + process.exit(1); + } + `, + ], + { encoding: 'utf8' } + ); + + const output = result.stdout + result.stderr; + + assert.ok( + output.includes('PASS: Got expected error'), + 'Should pass with expected error for undefined. Output: ' + output + ); + + assert.strictEqual( + result.status, + 0, + 'Process should exit with code 0. Output: ' + output + ); +} + +// Test case: Loader returning empty string source +{ + const result = spawnSync( + process.execPath, + [ + '--no-warnings', + '--input-type=module', + '--eval', + ` + import { register } from 'node:module'; + + const code = 'export function load(url, context, next) {' + + ' if (url.includes("test-empty-source")) {' + + ' return { format: "commonjs", source: "", shortCircuit: true };' + + ' }' + + ' return next(url);' + + '}'; + + register('data:text/javascript,' + encodeURIComponent(code)); + + try { + await import('file:///test-empty-source.js'); + console.log('ERROR: Should have thrown'); + process.exit(1); + } catch (err) { + if (err.code === 'ERR_INTERNAL_ASSERTION') { + console.log('FAIL: Got ERR_INTERNAL_ASSERTION'); + process.exit(1); + } + if (err.code === 'ERR_INVALID_RETURN_PROPERTY_VALUE') { + console.log('PASS: Got expected error'); + process.exit(0); + } + console.log('ERROR: Got unexpected error:', err.code); + process.exit(1); + } + `, + ], + { encoding: 'utf8' } + ); + + const output = result.stdout + result.stderr; + + assert.ok( + output.includes('PASS: Got expected error'), + 'Should pass with expected error for empty string. Output: ' + output + ); + + assert.strictEqual( + result.status, + 0, + 'Process should exit with code 0. Output: ' + output + ); +} + +console.log('All tests passed!'); From 883566390930dbedf7cffb8235aa5fcea08788b3 Mon Sep 17 00:00:00 2001 From: Vivek Pandey <98891756+vivek958@users.noreply.github.com> Date: Thu, 30 Oct 2025 01:15:33 +0530 Subject: [PATCH 03/14] Update test/parallel/test-esm-loader-null-source.js Co-authored-by: Antoine du Hamel --- test/parallel/test-esm-loader-null-source.js | 18 +----------------- 1 file changed, 1 insertion(+), 17 deletions(-) diff --git a/test/parallel/test-esm-loader-null-source.js b/test/parallel/test-esm-loader-null-source.js index 788e38e5003666..6eab317554c566 100644 --- a/test/parallel/test-esm-loader-null-source.js +++ b/test/parallel/test-esm-loader-null-source.js @@ -31,23 +31,7 @@ const fixtures = require('../common/fixtures'); register('data:text/javascript,' + encodeURIComponent(code)); - try { - await import('file:///test-null-source.js'); - console.log('ERROR: Should have thrown'); - process.exit(1); - } catch (err) { - // Should throw ERR_INVALID_RETURN_PROPERTY_VALUE, not ERR_INTERNAL_ASSERTION - if (err.code === 'ERR_INTERNAL_ASSERTION') { - console.log('FAIL: Got ERR_INTERNAL_ASSERTION'); - process.exit(1); - } - if (err.code === 'ERR_INVALID_RETURN_PROPERTY_VALUE') { - console.log('PASS: Got expected error'); - process.exit(0); - } - console.log('ERROR: Got unexpected error:', err.code); - process.exit(1); - } + await assert.rejects(import('file:///test-null-source.js'), { code: 'ERR_INVALID_RETURN_PROPERTY_VALUE' }); `, ], { encoding: 'utf8' } From 4d2299a15d25f2ceff631442105e625bc24aadd5 Mon Sep 17 00:00:00 2001 From: Vivek Pandey <98891756+vivek958@users.noreply.github.com> Date: Thu, 30 Oct 2025 01:15:55 +0530 Subject: [PATCH 04/14] Update test/parallel/test-esm-loader-null-source.js Co-authored-by: Antoine du Hamel --- test/parallel/test-esm-loader-null-source.js | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/test/parallel/test-esm-loader-null-source.js b/test/parallel/test-esm-loader-null-source.js index 6eab317554c566..5f490568c00fc7 100644 --- a/test/parallel/test-esm-loader-null-source.js +++ b/test/parallel/test-esm-loader-null-source.js @@ -12,6 +12,12 @@ const fixtures = require('../common/fixtures'); // Test case: Loader returning null source for CommonJS module // This should throw ERR_INVALID_RETURN_PROPERTY_VALUE, not ERR_INTERNAL_ASSERTION { + function load(url, context, next) { + if (url.includes("test-null-source")) { + return { format: "commonjs", source: null, shortCircuit: true }; + } + return next(url); + } const result = spawnSync( process.execPath, [ @@ -22,14 +28,7 @@ const fixtures = require('../common/fixtures'); import { register } from 'node:module'; // Register a custom loader that returns null source - const code = 'export function load(url, context, next) {' + - ' if (url.includes("test-null-source")) {' + - ' return { format: "commonjs", source: null, shortCircuit: true };' + - ' }' + - ' return next(url);' + - '}'; - - register('data:text/javascript,' + encodeURIComponent(code)); + register('data:text/javascript,export ' + encodeURIComponent(${load})); await assert.rejects(import('file:///test-null-source.js'), { code: 'ERR_INVALID_RETURN_PROPERTY_VALUE' }); `, From 8da8e2bef3d4fff49e3348f21beecec20e6a4db6 Mon Sep 17 00:00:00 2001 From: Vivek Pandey <98891756+vivek958@users.noreply.github.com> Date: Fri, 31 Oct 2025 08:20:44 +0530 Subject: [PATCH 05/14] Refactor source validation to check for null and undefined --- lib/internal/modules/esm/translators.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/internal/modules/esm/translators.js b/lib/internal/modules/esm/translators.js index a97d4116c4c3fc..0df94f4b926de4 100644 --- a/lib/internal/modules/esm/translators.js +++ b/lib/internal/modules/esm/translators.js @@ -128,7 +128,7 @@ function loadCJSModule(module, source, url, filename, isMain) { // Without this check, null or undefined source causes ERR_INTERNAL_ASSERTION // when passed to compileFunctionForCJSLoader. // Refs: https://github.com/nodejs/node/issues/60401 - if (source == null || source === '') { + if (source === null || source === undefined) { throw new ERR_INVALID_RETURN_PROPERTY_VALUE( 'non-empty string', 'load', From 34c41a113fad02c71404bcdcde1d342d1e4894e6 Mon Sep 17 00:00:00 2001 From: Vivek Pandey <98891756+vivek958@users.noreply.github.com> Date: Fri, 31 Oct 2025 08:41:11 +0530 Subject: [PATCH 06/14] Add empty test-null-source.js file --- test/fixtures/test-null-source.js | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 test/fixtures/test-null-source.js diff --git a/test/fixtures/test-null-source.js b/test/fixtures/test-null-source.js new file mode 100644 index 00000000000000..867f629c0efaf2 --- /dev/null +++ b/test/fixtures/test-null-source.js @@ -0,0 +1,2 @@ +// test/fixtures/test-null-source.js +export {}; From 0c311c49095d2844b95af11274d42800b060da87 Mon Sep 17 00:00:00 2001 From: Vivek Pandey <98891756+vivek958@users.noreply.github.com> Date: Fri, 31 Oct 2025 08:43:49 +0530 Subject: [PATCH 07/14] Update test-esm-loader-null-source.js --- test/parallel/test-esm-loader-null-source.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/test/parallel/test-esm-loader-null-source.js b/test/parallel/test-esm-loader-null-source.js index 5f490568c00fc7..a96d35eb796f41 100644 --- a/test/parallel/test-esm-loader-null-source.js +++ b/test/parallel/test-esm-loader-null-source.js @@ -1,9 +1,11 @@ 'use strict'; +import { pathToFileURL } from 'url'; +import path from 'path'; // Test that ESM loader handles null/undefined source gracefully // and throws meaningful error instead of ERR_INTERNAL_ASSERTION. // Refs: https://github.com/nodejs/node/issues/60401 - +const fixturePath = pathToFileURL(path.join(__dirname, '../fixtures/test-null-source.js')).href; const common = require('../common'); const assert = require('assert'); const { spawnSync } = require('child_process'); @@ -127,7 +129,7 @@ const fixtures = require('../common/fixtures'); register('data:text/javascript,' + encodeURIComponent(code)); try { - await import('file:///test-empty-source.js'); + await import(fixturePath); console.log('ERROR: Should have thrown'); process.exit(1); } catch (err) { From 0d6e7188702cf48f7ef7fef4ed88f273b89e8b35 Mon Sep 17 00:00:00 2001 From: Vivek Pandey <98891756+vivek958@users.noreply.github.com> Date: Sun, 4 Jan 2026 21:57:15 +0530 Subject: [PATCH 08/14] Update lib/internal/modules/esm/translators.js Co-authored-by: Antoine du Hamel --- lib/internal/modules/esm/translators.js | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/internal/modules/esm/translators.js b/lib/internal/modules/esm/translators.js index 9b549bcfff803a..e3370c0c425720 100644 --- a/lib/internal/modules/esm/translators.js +++ b/lib/internal/modules/esm/translators.js @@ -143,7 +143,6 @@ function loadCJSModule(module, source, url, filename, isMain) { } const compileResult = compileFunctionForCJSLoader(source, filename, false /* is_sea_main */, false); - const { function: compiledWrapper, sourceMapURL, sourceURL } = compileResult; // Cache the source map for the cjs module if present. if (sourceMapURL) { From 2a3ae6138b2264288c94e3616a1865b2a4d9c021 Mon Sep 17 00:00:00 2001 From: Vivek Pandey <98891756+vivek958@users.noreply.github.com> Date: Sun, 4 Jan 2026 21:57:34 +0530 Subject: [PATCH 09/14] Update test/parallel/test-esm-loader-null-source.js Co-authored-by: Antoine du Hamel --- test/parallel/test-esm-loader-null-source.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/parallel/test-esm-loader-null-source.js b/test/parallel/test-esm-loader-null-source.js index a96d35eb796f41..f271db956ce736 100644 --- a/test/parallel/test-esm-loader-null-source.js +++ b/test/parallel/test-esm-loader-null-source.js @@ -5,7 +5,7 @@ import path from 'path'; // Test that ESM loader handles null/undefined source gracefully // and throws meaningful error instead of ERR_INTERNAL_ASSERTION. // Refs: https://github.com/nodejs/node/issues/60401 -const fixturePath = pathToFileURL(path.join(__dirname, '../fixtures/test-null-source.js')).href; +const fixtureURL = fixtures.fileURL('test-null-source.js'); const common = require('../common'); const assert = require('assert'); const { spawnSync } = require('child_process'); From ef2feb2b7acb6ee163d1bec831973bc07829c893 Mon Sep 17 00:00:00 2001 From: Vivek Pandey <98891756+vivek958@users.noreply.github.com> Date: Sun, 4 Jan 2026 21:58:05 +0530 Subject: [PATCH 10/14] Update test/parallel/test-esm-loader-null-source.js Co-authored-by: Antoine du Hamel --- test/parallel/test-esm-loader-null-source.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/parallel/test-esm-loader-null-source.js b/test/parallel/test-esm-loader-null-source.js index f271db956ce736..d861d971ea4d3c 100644 --- a/test/parallel/test-esm-loader-null-source.js +++ b/test/parallel/test-esm-loader-null-source.js @@ -41,9 +41,9 @@ const fixtures = require('../common/fixtures'); const output = result.stdout + result.stderr; // Verify test passed - assert.ok( - output.includes('PASS: Got expected error'), - 'Should pass with expected error. Output: ' + output + assert.match( + output, + /PASS: Got expected error/, ); assert.strictEqual( From b8c492b60c93ecd82c8da9bd7f6157a94c571871 Mon Sep 17 00:00:00 2001 From: Vivek Pandey <98891756+vivek958@users.noreply.github.com> Date: Sun, 4 Jan 2026 22:06:15 +0530 Subject: [PATCH 11/14] Update source validation to allow empty string Change validation check for source to allow empty string. --- lib/internal/modules/esm/translators.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/internal/modules/esm/translators.js b/lib/internal/modules/esm/translators.js index e3370c0c425720..b63d3d8b5f21d0 100644 --- a/lib/internal/modules/esm/translators.js +++ b/lib/internal/modules/esm/translators.js @@ -133,7 +133,7 @@ function loadCJSModule(module, source, url, filename, isMain) { // Without this check, null or undefined source causes ERR_INTERNAL_ASSERTION // when passed to compileFunctionForCJSLoader. // Refs: https://github.com/nodejs/node/issues/60401 - if (source === null || source === undefined) { + if (source === null || source === '') { throw new ERR_INVALID_RETURN_PROPERTY_VALUE( 'non-empty string', 'load', From 5df58e7d0ddbd4a5c3bfc8bb509b484442635dfd Mon Sep 17 00:00:00 2001 From: Vivek Pandey <98891756+vivek958@users.noreply.github.com> Date: Sun, 4 Jan 2026 22:07:00 +0530 Subject: [PATCH 12/14] Delete test/fixtures/test-null-source.js --- test/fixtures/test-null-source.js | 2 -- 1 file changed, 2 deletions(-) delete mode 100644 test/fixtures/test-null-source.js diff --git a/test/fixtures/test-null-source.js b/test/fixtures/test-null-source.js deleted file mode 100644 index 867f629c0efaf2..00000000000000 --- a/test/fixtures/test-null-source.js +++ /dev/null @@ -1,2 +0,0 @@ -// test/fixtures/test-null-source.js -export {}; From e1b1b6f5cc6184ffc94bc49398a12a5ead724d13 Mon Sep 17 00:00:00 2001 From: Vivek Pandey <98891756+vivek958@users.noreply.github.com> Date: Sun, 4 Jan 2026 22:09:54 +0530 Subject: [PATCH 13/14] Refactor ESM loader tests with reusable functions Refactor ESM loader tests to use reusable loader functions for null, undefined, and empty source cases. --- test/parallel/test-esm-loader-null-source.js | 173 ++++++++----------- 1 file changed, 69 insertions(+), 104 deletions(-) diff --git a/test/parallel/test-esm-loader-null-source.js b/test/parallel/test-esm-loader-null-source.js index d861d971ea4d3c..3e57eae08a4a91 100644 --- a/test/parallel/test-esm-loader-null-source.js +++ b/test/parallel/test-esm-loader-null-source.js @@ -1,25 +1,46 @@ 'use strict'; -import { pathToFileURL } from 'url'; -import path from 'path'; // Test that ESM loader handles null/undefined source gracefully // and throws meaningful error instead of ERR_INTERNAL_ASSERTION. // Refs: https://github.com/nodejs/node/issues/60401 -const fixtureURL = fixtures.fileURL('test-null-source.js'); + const common = require('../common'); const assert = require('assert'); const { spawnSync } = require('child_process'); const fixtures = require('../common/fixtures'); -// Test case: Loader returning null source for CommonJS module -// This should throw ERR_INVALID_RETURN_PROPERTY_VALUE, not ERR_INTERNAL_ASSERTION -{ - function load(url, context, next) { - if (url.includes("test-null-source")) { - return { format: "commonjs", source: null, shortCircuit: true }; - } - return next(url); - } +// Reusable loader functions +function createNullLoader() { + return function load(url, context, next) { + if (url.includes('test-null-source')) { + return { format: 'commonjs', source: null, shortCircuit: true }; + } + return next(url); + }; +} + +function createUndefinedLoader() { + return function load(url, context, next) { + if (url.includes('test-undefined-source')) { + return { format: 'commonjs', source: undefined, shortCircuit: true }; + } + return next(url); + }; +} + +function createEmptyLoader() { + return function load(url, context, next) { + if (url.includes('test-empty-source')) { + return { format: 'commonjs', source: '', shortCircuit: true }; + } + return next(url); + }; +} + +// Helper to run test with custom loader +function runTestWithLoader(loaderFn, testUrl) { + const loaderCode = `export ${loaderFn.toString()}`; + const result = spawnSync( process.execPath, [ @@ -28,24 +49,36 @@ const fixtures = require('../common/fixtures'); '--eval', ` import { register } from 'node:module'; + import assert from 'node:assert'; - // Register a custom loader that returns null source - register('data:text/javascript,export ' + encodeURIComponent(${load})); + register('data:text/javascript,' + encodeURIComponent(${JSON.stringify(loaderCode)})); - await assert.rejects(import('file:///test-null-source.js'), { code: 'ERR_INVALID_RETURN_PROPERTY_VALUE' }); + await assert.rejects( + import(${JSON.stringify(testUrl)}), + { code: 'ERR_INVALID_RETURN_PROPERTY_VALUE' } + ); `, ], { encoding: 'utf8' } ); + return result; +} + +// Test case 1: Loader returning null source +{ + const result = runTestWithLoader( + createNullLoader(), + 'file:///test-null-source.js' + ); + const output = result.stdout + result.stderr; - // Verify test passed - assert.match( - output, - /PASS: Got expected error/, + assert.ok( + !output.includes('ERR_INTERNAL_ASSERTION'), + 'Should not throw ERR_INTERNAL_ASSERTION. Output: ' + output ); - + assert.strictEqual( result.status, 0, @@ -53,54 +86,20 @@ const fixtures = require('../common/fixtures'); ); } -// Test case: Loader returning undefined source +// Test case 2: Loader returning undefined source { - const result = spawnSync( - process.execPath, - [ - '--no-warnings', - '--input-type=module', - '--eval', - ` - import { register } from 'node:module'; - - const code = 'export function load(url, context, next) {' + - ' if (url.includes("test-undefined-source")) {' + - ' return { format: "commonjs", source: undefined, shortCircuit: true };' + - ' }' + - ' return next(url);' + - '}'; - - register('data:text/javascript,' + encodeURIComponent(code)); - - try { - await import('file:///test-undefined-source.js'); - console.log('ERROR: Should have thrown'); - process.exit(1); - } catch (err) { - if (err.code === 'ERR_INTERNAL_ASSERTION') { - console.log('FAIL: Got ERR_INTERNAL_ASSERTION'); - process.exit(1); - } - if (err.code === 'ERR_INVALID_RETURN_PROPERTY_VALUE') { - console.log('PASS: Got expected error'); - process.exit(0); - } - console.log('ERROR: Got unexpected error:', err.code); - process.exit(1); - } - `, - ], - { encoding: 'utf8' } + const result = runTestWithLoader( + createUndefinedLoader(), + 'file:///test-undefined-source.js' ); const output = result.stdout + result.stderr; assert.ok( - output.includes('PASS: Got expected error'), - 'Should pass with expected error for undefined. Output: ' + output + !output.includes('ERR_INTERNAL_ASSERTION'), + 'Should not throw ERR_INTERNAL_ASSERTION. Output: ' + output ); - + assert.strictEqual( result.status, 0, @@ -108,59 +107,25 @@ const fixtures = require('../common/fixtures'); ); } -// Test case: Loader returning empty string source +// Test case 3: Loader returning empty string source { - const result = spawnSync( - process.execPath, - [ - '--no-warnings', - '--input-type=module', - '--eval', - ` - import { register } from 'node:module'; - - const code = 'export function load(url, context, next) {' + - ' if (url.includes("test-empty-source")) {' + - ' return { format: "commonjs", source: "", shortCircuit: true };' + - ' }' + - ' return next(url);' + - '}'; - - register('data:text/javascript,' + encodeURIComponent(code)); - - try { - await import(fixturePath); - console.log('ERROR: Should have thrown'); - process.exit(1); - } catch (err) { - if (err.code === 'ERR_INTERNAL_ASSERTION') { - console.log('FAIL: Got ERR_INTERNAL_ASSERTION'); - process.exit(1); - } - if (err.code === 'ERR_INVALID_RETURN_PROPERTY_VALUE') { - console.log('PASS: Got expected error'); - process.exit(0); - } - console.log('ERROR: Got unexpected error:', err.code); - process.exit(1); - } - `, - ], - { encoding: 'utf8' } + const fixtureURL = fixtures.fileURL('es-modules/loose.js'); + + const result = runTestWithLoader( + createEmptyLoader(), + fixtureURL.href ); const output = result.stdout + result.stderr; assert.ok( - output.includes('PASS: Got expected error'), - 'Should pass with expected error for empty string. Output: ' + output + !output.includes('ERR_INTERNAL_ASSERTION'), + 'Should not throw ERR_INTERNAL_ASSERTION. Output: ' + output ); - + assert.strictEqual( result.status, 0, 'Process should exit with code 0. Output: ' + output ); } - -console.log('All tests passed!'); From a6b8e7b08f0003f2dcfd47cc3c5b5bfd9420ba27 Mon Sep 17 00:00:00 2001 From: Vivek Pandey <98891756+vivek958@users.noreply.github.com> Date: Sun, 4 Jan 2026 22:18:57 +0530 Subject: [PATCH 14/14] Update translators.js --- lib/internal/modules/esm/translators.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/internal/modules/esm/translators.js b/lib/internal/modules/esm/translators.js index b63d3d8b5f21d0..c2ca465c905103 100644 --- a/lib/internal/modules/esm/translators.js +++ b/lib/internal/modules/esm/translators.js @@ -133,7 +133,7 @@ function loadCJSModule(module, source, url, filename, isMain) { // Without this check, null or undefined source causes ERR_INTERNAL_ASSERTION // when passed to compileFunctionForCJSLoader. // Refs: https://github.com/nodejs/node/issues/60401 - if (source === null || source === '') { + if (source === null || source === undefined || source === '') { throw new ERR_INVALID_RETURN_PROPERTY_VALUE( 'non-empty string', 'load',