Maaz Zindani

Loading the index…

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

Adding a property to an object fails with 'object is not extensible'

ErrorTypeError: Cannot add property x, object is not extensible

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

  • Applies to Node.js 22, React 19, Redux Toolkit 2
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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));
  2. 2Match what you see, then jump to its fix

    1. 1The object came from useSelector or a store, and the error fires outside a reducerRedux Toolkit froze the state that you then mutateFix
    2. 2Object.isExtensible(obj) prints false and no Redux is involvedThe object was frozen, sealed or made non-extensible on purpose or by a libraryFix
    3. 3The error follows array.map(item => { item.newKey = value; return item })Mutating an item inside an array held in React stateFix

What does the error look like?

  • TypeError: Cannot add property x, object is not extensible
  • TypeError: Cannot define property x, object is not extensible
  • can'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=*.tsx

2Fix

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 dev

Confirmed by redux.js.org

Did this fix it?

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" src

2Fix

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

Did this fix it?

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

2Fix

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 dev

Confirmed by react.dev

Did this fix it?

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:

Related fixes

Next step

Want this handled for good?

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