SyntaxError: Missing initializer in const declaration
SyntaxError: Missing initializer in const declarationShort 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.
Fix it fast
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/2Match what you see, then jump to its fix
What does the error look like?
SyntaxError: Missing initializer in const declarationSyntaxError: missing = in const declarationconst 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 buildConfirmed by developer.mozilla.org
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.jsx2Fix
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 buildConfirmed by developer.mozilla.org
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- WorkforceA field app, an admin console and a self-hosted Node.js server for push and reconciliation.
Related fixes
Web apps
React shows Attempted import error when importing a component or function
Attempted import error:
The build found the file but not the name you asked for.
2 causes, each with a fixWeb apps
AxiosError Network Error with code ERR_NETWORK in a React app
Network Error
ERR_NETWORK means the request produced no readable response.
4 causes, each with a fixWeb apps
Axios get works with a URL string but ignores the object passed as the second argument
The server log shows a GET with no query string
axios.get takes a URL and an optional config object.
3 causes, each with a fixWeb apps
Can't resolve '../axios' in a React project
Module not found: Error: Can't resolve '../axios'
A path that starts with ./ or ../ is a file path, not the axios package.
3 causes, each with a fixWeb apps
Axios returns 401 from a React app but the login works elsewhere
Error: Request failed with status code 401
A 401 means the server received the request without valid credentials.
3 causes, each with a fixWeb apps
An axios call never finishes when the API server stops answering
code: 'ECONNABORTED'
Axios has no timeout unless you give it one: the default is 0, which means wait forever.
3 causes, each with a fix