Sometimes, we may run into the ‘TypeError: can’t redefine non-configurable property "x"’ when we’re developing JavaScript apps.
In this article, we’ll look at how to fix the ‘TypeError: can’t redefine non-configurable property "x"’ when we’re developing JavaScript apps.
Fix the ‘TypeError: can’t redefine non-configurable property "x"’ When Developing JavaScript Apps
To fix the ‘TypeError: can’t redefine non-configurable property "x"’ when we’re developing JavaScript apps, we should make sure we aren’t trying to redefine a non-configurable property with Object.defineProperty
.
For instance, we’ll get the error if we have:
const obj = Object.create({});
Object.defineProperty(obj, "foo", {value: "bar"});
Object.defineProperty(obj, "foo", {value: "baz"});
where we tried to redefine the non-configurable foo
property on obj
.
To fix this, we should make foo
configurable:
const obj = Object.create({});
Object.defineProperty(obj, "foo", {value: "bar", configurable: true});
Object.defineProperty(obj, "foo", {value: "baz", configurable: true}
Conclusion
To fix the ‘TypeError: can’t redefine non-configurable property "x"’ when we’re developing JavaScript apps, we should make sure we aren’t trying to redefine a non-configurable property with Object.defineProperty
.