Skip to content

module: detect module syntax in --check for stdin - #68016

Open
ktfth wants to merge 1 commit into
nodejs:mainfrom
ktfth:check-stdin-detect
Open

ktfth wants to merge 1 commit into
nodejs:mainfrom
ktfth:check-stdin-detect

Conversation

@ktfth

@ktfth ktfth commented Dec 4, 2026

Copy link
Copy Markdown

#65202 made node --check detect module syntax in ambiguous .js files, so it agrees with how they run. The stdin path was left out: with no --input-type, check_syntax.js always picks CommonJS for [stdin], while running the same code with node - detects the module syntax and runs it as ESM.

$ echo 'import fs from "node:fs"; console.log(1)' | node -
1
$ echo 'import fs from "node:fs"; console.log(1)' | node --check -
[stdin]:1
import fs from "node:fs"; console.log(1)
^^^^^^

SyntaxError: Cannot use import statement outside a module

It also means --check reports the wrong error for module code with a real syntax error: it stops at the import instead of pointing at the actual problem.

This picks the goal for stdin the way evalScript() does in shouldUseModuleEntryPoint(): it calls containsModuleSyntax() only when --input-type is not given and --experimental-detect-module is on, and it passes 'no CJS variables' because stdin code runs without require, module and friends in scope. --input-type=module, --input-type=commonjs and --no-experimental-detect-module behave as before.

Tests

  • test-cli-syntax-piped-good.js: module code without --input-type passes and is not run.
  • test-cli-syntax-piped-bad.js:
    • module code with a syntax error reports that error;
    • with --input-type=commonjs or --no-experimental-detect-module, module syntax is still rejected.

The new cases fail without the change.

Not included: --check still rejects TypeScript (files and --input-type=*-typescript), because type stripping happens only on the run path. That is a separate change.

🤖 Generated with Claude Code

https://claude.ai/code/session_0135Ju6jz832jWhjv7Yq5xwy

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: nodejs#65202
Signed-off-by: Kaique da Silva <kaeyosthaeron@gmail.com>
@nodejs-github-bot nodejs-github-bot added the needs-ci PRs that need a full CI run. label Dec 4, 2026
@github-actions

github-actions Bot commented Dec 4, 2026

Copy link
Copy Markdown
Contributor

Welcome to Node.js, and thank you for your first contribution!

Before review, please take a moment to read:

Please make sure every commit is signed off. For a first pull request, GitHub Actions require collaborator approval and Jenkins CI must be started by a collaborator or triager, so an initial wait is normal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants