Adding a property to an object fails with 'object is not extensible'
TypeError: Cannot add property x, object is not extensibleShort answer
The error means the object was sealed, frozen or marked non-extensible, so no new property can be added. In React apps the usual source is state that Redux Toolkit's Immer froze, or an object a library froze. Copy the object, add the property to the copy, and use the copy.
Fix it fast
1Find out which cause you have
Ask the engine whether the object is frozen, sealed or only non-extensible. This tells you if something called Object.freeze on it.
console.log(Object.isFrozen(item), Object.isSealed(item), Object.isExtensible(item));2Match what you see, then jump to its fix
- 1
The object came from useSelector or a store, and the error fires outside a reducerRedux Toolkit froze the state that you then mutateFix - 2
Object.isExtensible(obj) prints false and no Redux is involvedThe object was frozen, sealed or made non-extensible on purpose or by a libraryFix - 3
The error follows array.map(item => { item.newKey = value; return item })Mutating an item inside an array held in React stateFix
- 1
What does the error look like?
TypeError: Cannot add property x, object is not extensibleTypeError: Cannot define property x, object is not extensiblecan't define property "x": Object is not extensible
Cause 1 of 3
Redux Toolkit froze the state that you then mutate
1Check
Redux Toolkit uses Immer, which freezes state automatically. The docs say this makes accidental mutation throw an error, even outside of a reducer.
grep -rn "useSelector\|\.push(\|\.sort(" src --include=*.jsx --include=*.tsx2Fix
Copy the item before adding the property. Inside a createSlice reducer, mutate the draft directly; outside it, copy.
const withRating = items.map(item => ({ ...item, rating: 5 }));
// inside a slice reducer, mutating the draft is fine
ratingAdded(state, action) {
const item = state.items.find(i => i.id === action.payload);
item.rating = 5;
}3Verify
Run the screen again; the new property shows on the copied items.
npm run devConfirmed by redux.js.org
Cause 2 of 3
The object was frozen, sealed or made non-extensible on purpose or by a library
1Check
MDN lists Object.preventExtensions(), Object.seal() and Object.freeze() as the ways an object becomes non-extensible, and notes that a library you use may have called one of them.
grep -rn "Object.freeze\|Object.seal\|Object.preventExtensions" src2Fix
Copy the properties into a new object and add the new property to the copy, or add the property before the object is made non-extensible.
const copy = { ...frozenObj, x: "foo" };3Verify
The assignment now succeeds on the copy.
console.log(Object.isExtensible(copy));Confirmed by developer.mozilla.org
Cause 3 of 3
Mutating an item inside an array held in React state
1Check
Copying an array is shallow, so the items inside still point at the original objects; the React docs say to mutate only objects you have just created.
grep -rn -B2 -A2 "\.map(" src/components/List.jsx2Fix
Return a copy of each item you change, not the same object.
setList(list.map(item =>
item.id === id ? { ...item, seen: true } : item
));3Verify
Click the control; the list updates and the console stays clean.
npm run devConfirmed by react.dev
How do I stop it coming back?
- Treat state, props and selector results as read-only; copy before you change.
- Use createSlice reducers for Redux changes, since the mutating style is safe only inside Immer.
- Do not call Object.freeze on objects you hand to other code that may add fields.
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