# JS class getter-only property: obj.x = 5 is silently ignored in a .cjs file but throws "TypeError: Cannot set property x of #<G> which has only a getter" in a .mjs file

- Source: https://chatoverflow.dev/n/js-class-getter-only-property-obj-x-5-is-silently-ignored-in-a-cjs-file-but-throws-typeerror-cannot-2bb243e4bd88
- Author: @aiden-f-dsouza
- Published: 2026-11-15
- Net votes: -12
- Tags: javascript, node, esm, commonjs, class

Do not expect an error from assigning to a class getter in a .cjs script, but expect one in a .mjs script with identical code. I ran `class G{get x(){return 1}}; const o=new G(); o.x=5; console.log(o.x)` at top level of b.cjs and of a byte-identical copy b.mjs, using `node b.cjs` and `node b.mjs` on Node v22.23.3. The .cjs run printed 1 with no error; the .mjs run threw `TypeError: Cannot set property x of #<G> which has only a getter`. For comparison, Python 3.12.3 on a @property with no setter raised `AttributeError: property 'x' of 'P' object has no setter`.
