Assignment to constant variable
The code tried to give a new value to a variable declared with const, which can only be set once.
Also appears as
- TypeError: invalid assignment to const 'count'
- TypeError: Attempted to assign to readonly property.
Prompt to fix it
My app throws "TypeError: Assignment to constant variable." Find the line that reassigns a const and fix it the right way for that case: use let for local variables that really change, call the state setter for React state instead of assigning to it, use let in counting for loops, and export an update function instead of assigning to an imported value. Don’t change every const in the file to let.
Paste into Layerwise or any AI coding tool.
What it means
const creates a name that always points at the same value. Any later =, +=, or ++ on that name throws this error when the line runs. let creates a name you can point at a new value.
const only locks the name. The inside of a const object or array can still change, so todos.push(todo) and user.name = 'Ana' work. Imports behave like const too: assigning to an imported name throws the same error.
In React, the right fix is usually the state setter. A component that does count = count + 1 on a value from useState needs setCount(count + 1), because React re-renders only when state changes through its setter. Switching to let there removes the error and leaves the screen showing the old value.
Common causes
- A
constis reassigned later, often a counter, a flag likeisLoading, or a result that an AI edit decided to update. - A loop is written as
for (const i = 0; i < n; i++), soi++tries to change a constant. - React state from
useStateis changed with=instead of its setter. - Code assigns a new value to an imported name, which JavaScript treats as read-only.
How to fix it
- Open the line in the error and find the
=,+=, or++that targets theconst. - If a local variable really needs to change, declare it with
let. - For React state, call the setter, as in
setCount((c) => c + 1). - In a counting loop, use
let i.for (const item of items)is fine, because each pass gets a newitem. - For an imported value, export a function from its module that updates it, or move the value into state.
Example fix
import { useState } from 'react';
export function Cart({ prices }: { prices: number[] }) {
// A local value that changes: declare it with let.
let total = 0;
for (const price of prices) total += price;
// React state: copies = copies + 1 would throw. Call the setter instead.
const [copies, setCopies] = useState(1);
return (
<button onClick={() => setCopies((c) => c + 1)}>
Add a copy. Total: {total * copies}
</button>
);
}Related errors
All errorsX is not defined
The code used a name JavaScript can’t find anywhere, usually because of a missing import, a typo, or a Node-only global like process in the browser.
Type is not assignable to type
A value’s type doesn’t match what the variable, prop, or function expects, often because the value might be missing or is the wrong kind.
Too many re-renders
A component changes its own state while it renders, so React renders it again and again until it stops the loop.
Cannot read properties of undefined
The code asked for a property of a value that doesn’t exist yet, usually data that hasn’t loaded or a field the API didn’t return.
Build with an agent that checks its work
The Layerwise agent clicks through the live preview and reads the console, so it catches errors before you do.