Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

SyntaxError: Missing initializer in const declaration

ErrorSyntaxError: Missing initializer in const declaration

Short answer

A const must get its value in the same statement that declares it. The error appears when you write const without an equals sign and a value, which also happens when a type annotation or a typo breaks the statement. Add the value, or use let if the variable is assigned later.

  • Applies to Node.js 22, Chrome 130+, Firefox 132+, React 19
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    The build output names the file and line. Open it and look at that const: if there is no = and no value on it, you have the cause.

    grep -rnE "^\s*const [A-Za-z_$][A-Za-z0-9_$]*\s*;" src/
  2. 2Match what you see, then jump to its fix

    1. 1const COLUMNS; or const user; with nothing after the nameA const was declared without a valueFix
    2. 2The value is set inside an if, a try block or a callback after the declarationThe variable is meant to change later, so it should not be constFix

What does the error look like?

  • SyntaxError: Missing initializer in const declaration
  • SyntaxError: missing = in const declaration
  • const declared variable 'x' must have an initializer.

Cause 1 of 2

A const was declared without a value

1Check

JavaScript does not let a const be reassigned, so it must have its value where it is declared. V8 (Chrome, Node.js) prints Missing initializer in const declaration, Firefox prints missing = in const declaration.

grep -rnE "const [A-Za-z_$]+;" src/

2Fix

Put the value in the declaration.

// wrong
const COLUMNS;

// right
const COLUMNS = 80;

3Verify

Rebuild; the error is gone.

npm run build

Confirmed by developer.mozilla.org

Did this fix it?

Cause 2 of 2

The variable is meant to change later, so it should not be const

1Check

If the code assigns to the name later, const is the wrong keyword. let and var both allow a declaration without a value.

grep -rn "let \|const " src/components/Report.jsx

2Fix

Use let, and keep the assignment where it is.

let columns;
if (wide) {
  columns = 80;
} else {
  columns = 40;
}

3Verify

Rebuild and run the component.

npm run build

Confirmed by developer.mozilla.org

Did this fix it?

How do I stop it coming back?

  • Decide when you write the line: a value that never changes gets const with its value, a value set later gets let.
  • Run the editor's syntax check before committing, since this error stops the whole bundle.

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 built on the same technologies:

Related fixes

Next step

Want this handled for good?

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