Find syntax errors and exit status
Node distinguishes syntax checking from execution. Use node --check file.mjs to check whether a file can be parsed without running it. An exit status is a number returned by a process: zero conventionally means success, and a nonzero value indicates failure or another documented condition.
Local practice: The proposed teaching runtime is Node.js 26.13.1 LTS. These original offline examples were executed on Node.js 22.16.0 (Linux x64); execution on the proposed baseline and in ByteMind is not yet verified. No npm dependencies are required. Use a fresh practice directory. Code blocks are static, not a browser-based Node runner.
Check valid syntax without running the script
In the directory containing the files above, run:
node --check main.mjsExpected standard output: none.
Expected exit status: 0.
How it works
A syntax error occurs when source text violates JavaScript grammar, for example when a declaration lacks its initializer. The parser cannot produce executable instructions for that file. A runtime error occurs while executing syntactically valid code, such as calling an undefined function. Syntax checking does not prove that dependencies resolve, calculations are correct or runtime behavior is safe. Diagnostic messages are typically written to standard error. Exact wording and stack frames may change between releases.
Check deliberately invalid syntax
In the directory containing the files above, run:
node --check main.mjsExpected standard output: none.
Expected exit status: 1. Standard error contains SyntaxError (the rest of the diagnostic can vary).
Common mistake
The second fixture is intentionally invalid and is used only to demonstrate syntax checking. Do not confuse a zero exit status from --check with a successful application test. It checks syntax, not the result of program logic.
Try it yourself
Correct the invalid declaration to const quantity = 2;, run the check again, then print the value with a separate run. Describe why syntax checking and running are different operations.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Find syntax errors and exit status — knowledge check
Check your understanding of find syntax errors and exit status.
Does `node --check file.mjs` run the file’s console.log?
What is wrong with `const quantity = ;`?
What does a successful syntax check establish?