babel/babel · error · TypeError
"${name}" is read-only
Error message
"${name}" is read-only What it means
This TypeError is thrown by _readOnlyError(name), emitted by Babel's block-scoping transform when a `const`-declared binding is assigned in source. Because the transform may downlevel `const` to `var` (losing native read-only enforcement), it inserts this helper at assignment sites to preserve the runtime TypeError that `const` would normally produce. The thrown message names the offending binding.
Source
Thrown at packages/babel-helpers/src/helpers/readOnlyError.ts:4
/* @minVersion 7.0.0-beta.0 */
export default function _readOnlyError(name: string) {
throw new TypeError('"' + name + '" is read-only');
}
View on GitHub (pinned to 06b6eae39d)
Solutions
- Change the declaration from `const` to `let` if reassignment is intended.
- If the binding should be immutable, fix the logic so it never reassigns (mutate properties instead: `cfg.field = x`).
- Enable a linter rule (prefer-const / no-const-assign) to catch this at edit time.
Example fix
// before const total = 0; for (const n of nums) total += n; // throws // after let total = 0; for (const n of nums) total += n;
Defensive patterns
Strategy: validation
Validate before calling
// Decide intent before declaring: // - need reassignment? use `let` // - immutable binding? use `const` and never reassign let total = 0; total += n;
Prevention
- Enable ESLint `no-const-assign` to catch this at edit time.
- Use `let` whenever the variable will be reassigned.
- Mutate object fields rather than rebinding const references.
When it happens
Trigger: Any assignment to a `const` variable: `const x = 1; x = 2;`, `const cfg = {}; cfg = newCfg;`, `++counter` where counter is const, or compound assignment `x += 1` on a const.
Common situations: Accidentally declaring with `const` instead of `let`; refactoring that changes a value's mutation pattern; copy-paste leaving `const` on a variable that needs reassignment; linter disabled so the mistake reaches the transpiler.
Related errors
- Invalid attempt to destructure non-iterable instance. In ord
- Invalid attempt to spread non-iterable instance. In order to
- Cannot set property of null or undefined.
- Cannot destructure ${obj}
- Derived constructors may only return object or undefined
AI-assisted analysis of babel/babel@06b6eae39d (2026-08-03).
Data as JSON: /data/errors/390dd5cebf5dca1f.json.
Report an issue: GitHub.