Node.js Reference ยท CLI Flags
Node.js --check flag
Reference for node --check (-c): parse a script for syntax errors without executing it. Exit codes, ES modules, limits and use in CI and git hooks.
node --check file.js (short form -c) parses a file and exits. If the syntax is valid it prints nothing and exits with code 0. If not, it prints the error with the file name, line and a caret under the problem, and exits with code 1. The code itself is never executed, so nothing it would import, start or write happens.
It is a quick, dependency-free first line of defense in CI jobs, git hooks and editor tasks.
node --check app.js
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.
Syntax
node --check script.js node -c script.js node --check - < script.js
| Part | Description |
|---|---|
--check, -c |
Check syntax only. |
script.js |
The file to check. .mjs and .cjs files are parsed as modules and scripts; for .js, the nearest package.json type decides. |
- |
Read the code from standard input. |
Exit codes: 0 when the syntax is valid; 1 for a syntax error (the message goes to stderr). A file that cannot be read also exits with 1.
Usage notes
- Syntax only. It finds typos, unbalanced brackets and early errors such as duplicate
letdeclarations. It does not find runtime errors, wrong property names, missing modules or type problems; for those use tests, a linter or TypeScript. - One file at a time. It does not follow
require()orimport, so check each file separately, for example with a shell loop orgit ls-files '*.js' | xargs -n1 node --check. - Module versus script matters. The same text can be valid as a module and invalid as a script (top-level
await,import). Use the right extension ortypefield so the check matches how Node will really load it. - No TypeScript or JSX. Those syntaxes are invalid for
--check, except for files Node can strip types from when type stripping is available. - Output is silent on success. Rely on the exit status in scripts:
node --check app.js && echo ok. - Git hook idea: run it on staged
.jsfiles so broken syntax never reaches a commit.
Related
node file,-e and -p,--test- Lessons: Running code, Errors