Skip to main content

How to Resolve "TypeError: Cannot redefine property" Error in JavaScript

The TypeError: Cannot redefine property: X is an error that occurs when you attempt to modify or delete an object property that has been defined as non-configurable. This is a common issue when working with Object.defineProperty(), which creates properties with a more restricted configuration by default.

This guide will explain the concept of property descriptors in JavaScript, show you why this error occurs, and teach you how to correctly define your properties to make them configurable.

The Core Concept: Property Descriptors

Every property on a JavaScript object has a set of hidden attributes called property descriptors. These descriptors control how the property behaves. The main ones are:

  • value: The value of the property.
  • writable: If true, the value of the property can be changed. Defaults to false.
  • enumerable: If true, the property will show up during enumeration (e.g., in a for...in loop or Object.keys()). Defaults to false.
  • configurable: If true, the property can be deleted from the object, and its descriptors (other than value and writable) can be changed. Defaults to false.

When you create a property with a simple assignment (obj.prop = 'value'), writable, enumerable, and configurable all default to true. However, when you use Object.defineProperty(), they all default to false.

The Cause of the Error: A Non-Configurable Property

The "Cannot redefine property" error is thrown when you try to change the descriptors of, or delete, a property where configurable is false.

Problem: when you use Object.defineProperty() without specifying the descriptors, they default to false.

// Problem: The 'site' property is non-configurable by default.
const myObject = {};

// The `configurable` descriptor for 'site' defaults to false.
Object.defineProperty(myObject, 'site', {
value: 'tutorialreference.com'
});

// ⛔️ TypeError: Cannot redefine property: site
// We are trying to change the `value` descriptor of a non-configurable property.
Object.defineProperty(myObject, 'site', {
value: 'google.com'
});
note

Because the site property was created with configurable: false, you cannot redefine it.

The Solution: Set configurable: true

To solve the error, you must explicitly set configurable: true when you first define the property. This tells the JavaScript engine that the property's configuration can be changed later.

const myObject = {};

// ✅ Solution: Explicitly set `configurable: true`.
Object.defineProperty(myObject, 'site', {
value: 'tutorialreference.com',
writable: true, // Also set `writable` if you want to change the value
configurable: true
});

// Now, you can redefine it without an error.
Object.defineProperty(myObject, 'site', {
value: 'google.com',
writable: true,
configurable: true
});

console.log(myObject.site); // Output: "google.com"

// You can also now delete the property.
delete myObject.site;
console.log(myObject.site); // Output: undefined

configurable vs. writable: A Key Distinction

It's important to understand the difference between writable and configurable.

  • writable: true: Allows you to change the value of the property using a simple assignment (obj.prop = 'newValue'). It does not allow you to delete the property or change its enumerable or configurable status.
  • configurable: true: Allows you to delete the property and change all of its descriptors.
const obj = {};

Object.defineProperty(obj, 'prop', {
value: 'initial',
writable: true, // The value can be changed...
configurable: false // ...but the property cannot be deleted or redefined.
});

// ✅ This is allowed because `writable` is true.
obj.prop = 'new value';
console.log(obj.prop); // Output: "new value"

// ⛔️ This is NOT allowed because `configurable` is false.
delete obj.prop; // Throws a TypeError in strict mode.
note

If your goal is simply to create a property that can be changed later, setting writable: true might be enough. If you need the ability to delete or fully redefine it, you must use configurable: true.

Conclusion

The TypeError: Cannot redefine property is a direct consequence of an object property's configurability.

  • The error occurs when you try to modify or delete a property where the configurable descriptor is false.
  • When using Object.defineProperty(), the configurable, writable, and enumerable descriptors all default to false.
  • To solve the error, you must explicitly set configurable: true in the property descriptor when you first define the property.

By understanding and correctly using property descriptors, you can create objects with precisely the behavior and level of protection your application requires.