Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Node.js, PHP and APIs

SyntaxError when let is the body of an if, loop or label

Your error

SyntaxError: Lexical declaration cannot appear in a single-statement context

Fix it fast

Most common cause, 1 of 3

let is the only body of an if or else branch

Give the branch braces.

Fix
// 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);
Did this fix it?
Full steps for this cause: check, fix, verify

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.

  • Applies to Node.js 22, 24 and 26 (V8 parser), Chrome and other V8 browsers, ESLint curly rule
  • Updated
  • By Maaz Zindani, Senior Software Engineer

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.

Run this first
node --check src/bot.js

Then find the line you see in this list and open its fix:

  1. 1let is the only body of an if or else branchThe carets sit under let directly after if (...) or else, on the same line
  2. 2A one-line for or while loop has let as its bodyThe carets are under let right after for (...), for (... of ...) or while (...)
  3. 3A label lands on a let declarationThe 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.

Check
grep -nE "(if \(.*\)|else) +let " -r src

Fix

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.

Fix
// 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.

Verify
node --check src/bot.js && echo "parsed OK"

Confirmed by developer.mozilla.org

Did this fix it?

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.

Check
grep -nE "(for|while) \(.*\) +let " -r src

Fix

Add braces around the loop body. If the variable only feeds one call, you can also drop it and pass the expression straight in.

Fix
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.

Verify
node src/report.js

Confirmed by developer.mozilla.org

Did this fix it?

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.

Check
# 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 src

Fix

Move the declaration above the label, so the label sits directly in front of the loop that continue or break refers to.

Fix
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.

Verify
node --check src/import.js

Confirmed by developer.mozilla.org

Did this fix it?

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.

Which stack is behind this page?

Projects of mine on the same stack: TaxiDex, Workforce.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.