SyntaxError when let is the body of an if, loop or label
Your error
SyntaxError: Lexical declaration cannot appear in a single-statement contextFix it fast
Most common cause, 1 of 3let is the only body of an if or else branch
Give the branch braces.
// the declaration now lives in a block
if (message.content === '!ping') {
let reply = 'pong';
await message.reply(reply);
}
// value used after the if: declare first, assign in the branch
let reply = 'unknown command';
if (message.content === '!ping') reply = 'pong';
await message.reply(reply);Short answer
SyntaxError: Lexical declaration cannot appear in a single-statement context means a let declaration is the whole body of an if, else, loop or label, with no braces. let is a declaration, not a statement, so V8 refuses it there. Wrap the body in { }, or declare the variable before the if. Same in Node.js 22, 24 and 26.
Which cause do you have?
Let Node.js parse the file without running it. It prints the file and line number and draws carets under the let. The word just before the let (if, else, for, while, or a name followed by a colon) points to the matching cause below.
node --check src/bot.jsThen find the line you see in this list and open its fix:
- 1let is the only body of an if or else branch
The carets sit under let directly after if (...) or else, on the same line - 2A one-line for or while loop has let as its body
The carets are under let right after for (...), for (... of ...) or while (...) - 3A label lands on a let declaration
The line before the let, or the start of the same line, is a name and a colon such as outer:
What does the error look like?
SyntaxError: Lexical declaration cannot appear in a single-statement context
Cause 1 of 3
let is the only body of an if or else branch
You seeThe carets sit under let directly after if (...) or else, on the same line
Check
V8 throws Lexical declaration cannot appear in a single-statement context when let follows if (condition) or else with no block. Each arm of if...else accepts exactly one statement, and let opens a declaration, which is a different thing in the grammar. var counts as a statement, so an old one-liner with var parsed fine and breaks once someone changes it to let. Swap let for const in the same spot and V8 reports Unexpected token 'const' instead, because its parser gives the dedicated message only to let.
grep -nE "(if \(.*\)|else) +let " -r srcFix
Give the branch braces. Watch the scope: a variable declared inside braces disappears after the closing brace. When later code needs the value, declare the variable above the if and only assign it inside the branch.
// the declaration now lives in a block
if (message.content === '!ping') {
let reply = 'pong';
await message.reply(reply);
}
// value used after the if: declare first, assign in the branch
let reply = 'unknown command';
if (message.content === '!ping') reply = 'pong';
await message.reply(reply);Verify
The syntax check prints nothing and exits with status 0.
node --check src/bot.js && echo "parsed OK"Confirmed by developer.mozilla.org
Cause 2 of 3
A one-line for or while loop has let as its body
You seeThe carets are under let right after for (...), for (... of ...) or while (...)
Check
Loop bodies follow the same rule as if branches: MDN notes that most control-flow structures accept only a statement as their body. A loop written on one line, such as for (const row of rows) let sum = row.a + row.b, therefore fails to parse with the identical SyntaxError before any code runs.
grep -nE "(for|while) \(.*\) +let " -r srcFix
Add braces around the loop body. If the variable only feeds one call, you can also drop it and pass the expression straight in.
for (const row of rows) {
let sum = row.a + row.b;
totals.push(sum);
}
// or without the variable
for (const row of rows) totals.push(row.a + row.b);Verify
Run the script again. It gets past parsing, and the loop produces its output.
node src/report.jsConfirmed by developer.mozilla.org
Cause 3 of 3
A label lands on a let declaration
You seeThe line before the let, or the start of the same line, is a name and a colon such as outer:
Check
Labels attach only to statements, so name: let x = 0 is rejected with this error too. It usually happens when a label meant for a loop sits above a variable that was added later: the label now points at the let instead of the for, even when they are on separate lines.
# a label alone on its line, plus the line after it
grep -nE -A1 "^[[:space:]]*[A-Za-z_][A-Za-z0-9_]*:[[:space:]]*$" -r src
# a label and let on the same line
grep -nE "[A-Za-z_][A-Za-z0-9_]*: *let " -r srcFix
Move the declaration above the label, so the label sits directly in front of the loop that continue or break refers to.
let skipped = 0;
outer:
for (const row of rows) {
for (const cell of row) {
if (cell === null) { skipped++; continue outer; }
}
}Verify
node --check passes, and continue outer jumps to the next row as intended.
node --check src/import.jsConfirmed by developer.mozilla.org
How do I stop it coming back?
- Set ESLint's curly rule to "all" (its default); eslint --fix inserts the missing braces around every if, else, for, while and do body.
- Run node --check on changed .js files in a pre-commit hook or CI step, so a parse error is caught before the process restarts on a server.
- Keep a loop label on the line directly above its loop, and place new variables above the label.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.