From 8c160208a4c4b127e5d67afaea5bc6689796f69e Mon Sep 17 00:00:00 2001 From: Kaique da Silva Date: Fri, 4 Dec 2026 11:30:52 -0300 Subject: [PATCH] module: detect module syntax in --check for stdin Code piped to `node -` without `--input-type` runs as ES module when it contains module syntax, but `node --check -` always parsed it as CommonJS. Valid ES module code was rejected with "Cannot use import statement outside a module", which also hid the real error in code that does have one. Decide the goal the same way `evalScript()` does: look for module syntax when `--input-type` is not given and detection is enabled, with no CommonJS variables in scope. Refs: https://github.com/nodejs/node/issues/65202 Signed-off-by: Kaique da Silva --- lib/internal/main/check_syntax.js | 15 ++++++- test/parallel/test-cli-syntax-piped-bad.js | 46 +++++++++++++++++++++ test/parallel/test-cli-syntax-piped-good.js | 16 +++++++ 3 files changed, 76 insertions(+), 1 deletion(-) diff --git a/lib/internal/main/check_syntax.js b/lib/internal/main/check_syntax.js index 7bc7bbc1ede4..1482363c7ff0 100644 --- a/lib/internal/main/check_syntax.js +++ b/lib/internal/main/check_syntax.js @@ -59,7 +59,20 @@ function loadESMIfNeeded(cb) { async function checkSyntax(source, filename) { let format; if (filename === '[stdin]' || filename === '[eval]') { - format = (getOptionValue('--input-type') === 'module') ? 'module' : 'commonjs'; + const inputType = getOptionValue('--input-type'); + if (inputType === 'module') { + format = 'module'; + } else if (inputType === '' && + getOptionValue('--experimental-detect-module')) { + // Without `--input-type`, code from stdin runs as ESM when it contains + // module syntax. Decide the goal the same way here. There are no + // CommonJS variables in scope for this code, as for `--eval`. + const { containsModuleSyntax } = internalBinding('contextify'); + format = containsModuleSyntax(source, filename, null, 'no CJS variables') ? + 'module' : 'commonjs'; + } else { + format = 'commonjs'; + } } else { const { defaultResolve } = require('internal/modules/esm/resolve'); const { defaultGetFormat } = require('internal/modules/esm/get_format'); diff --git a/test/parallel/test-cli-syntax-piped-bad.js b/test/parallel/test-cli-syntax-piped-bad.js index 4af0aa2b6df7..616e1cb83687 100644 --- a/test/parallel/test-cli-syntax-piped-bad.js +++ b/test/parallel/test-cli-syntax-piped-bad.js @@ -54,3 +54,49 @@ syntaxArgs.forEach(function(arg) { assert.strictEqual(c.status, 1); }); + +// Without --input-type, module syntax is detected, so the error reported is +// the one in the code and not the one for parsing it as CommonJS. +syntaxArgs.forEach(function(arg) { + const stdin = 'import "node:fs"; var foo bar;'; + const c = spawnSync( + node, + ['--no-warnings', arg], + { encoding: 'utf8', input: stdin } + ); + + // stderr should include '[stdin]' as the filename + assert(c.stderr.startsWith('[stdin]'), `${c.stderr} starts with ${stdin}`); + + // No stdout should be produced + assert.strictEqual(c.stdout, ''); + + // stderr should have a syntax error message + assert.match(c.stderr, syntaxErrorRE); + + assert.strictEqual(c.status, 1); +}); + +// Module syntax is not detected with --input-type=commonjs or with +// --no-experimental-detect-module. +for (const flag of ['--input-type=commonjs', '--no-experimental-detect-module']) { + syntaxArgs.forEach(function(arg) { + const stdin = 'import "node:fs";'; + const c = spawnSync( + node, + [flag, '--no-warnings', arg], + { encoding: 'utf8', input: stdin } + ); + + // stderr should include '[stdin]' as the filename + assert(c.stderr.startsWith('[stdin]'), `${c.stderr} starts with ${stdin}`); + + // No stdout should be produced + assert.strictEqual(c.stdout, ''); + + // stderr should have a syntax error message + assert.match(c.stderr, /^SyntaxError: Cannot use import statement outside a module/m); + + assert.strictEqual(c.status, 1); + }); +} diff --git a/test/parallel/test-cli-syntax-piped-good.js b/test/parallel/test-cli-syntax-piped-good.js index db2e0f875d2a..fa2ed51224b1 100644 --- a/test/parallel/test-cli-syntax-piped-good.js +++ b/test/parallel/test-cli-syntax-piped-good.js @@ -40,3 +40,19 @@ syntaxArgs.forEach(function(arg) { assert.strictEqual(c.status, 0); }); + +// Without --input-type, module syntax is detected as it is when the code runs. +syntaxArgs.forEach(function(arg) { + const stdin = 'import "node:fs"; throw new Error("should not get run");'; + const c = spawnSync( + node, + ['--no-warnings', arg], + { encoding: 'utf8', input: stdin } + ); + + // No stdout or stderr should be produced + assert.strictEqual(c.stdout, ''); + assert.strictEqual(c.stderr, ''); + + assert.strictEqual(c.status, 0); +});