Skip to content

Commit ea08d7f

Browse files
authored
Validate source before compiling CJS module. module: fix ERR_INTERNAL_ASSERTION in CJS module loading
Fixes: #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.
1 parent 9be412f commit ea08d7f

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

lib/internal/modules/esm/translators.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,21 @@ translators.set('module', function moduleStrategy(url, translateContext, parentU
124124
* @param {boolean} isMain - Whether the module is the entrypoint
125125
*/
126126
function loadCJSModule(module, source, url, filename, isMain) {
127-
const compileResult = compileFunctionForCJSLoader(source, filename, false /* is_sea_main */, false);
127+
// Validate source before compilation to prevent internal assertion errors.
128+
// Without this check, null or undefined source causes ERR_INTERNAL_ASSERTION
129+
// when passed to compileFunctionForCJSLoader.
130+
// Refs: https://github.com/nodejs/node/issues/60401
131+
if (source == null || source === '') {
132+
throw new ERR_INVALID_RETURN_PROPERTY_VALUE(
133+
'non-empty string',
134+
'load',
135+
'source',
136+
source,
137+
);
138+
}
128139

140+
const compileResult = compileFunctionForCJSLoader(source, filename, false /* is_sea_main */, false);
141+
129142
const { function: compiledWrapper, sourceMapURL, sourceURL } = compileResult;
130143
// Cache the source map for the cjs module if present.
131144
if (sourceMapURL) {

0 commit comments

Comments
 (0)