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); +});