babel/babel · error · TypeError
Cannot initialize the same private elements twice on an obje
Error message
Cannot initialize the same private elements twice on an object
What it means
Runtime helper _checkPrivateRedeclaration, emitted when transpiling private fields/methods. Before initializing the per-instance WeakMap/WeakSet for a class's private collection, it asserts that this object has not already been initialized; if the private collection already has the object, it throws a TypeError. This enforces the spec rule that the same private fields cannot be installed twice on one instance.
Source
Thrown at packages/babel-helpers/src/helpers/checkPrivateRedeclaration.ts:8
/* @minVersion 7.14.1 */
export default function _checkPrivateRedeclaration(
obj: object,
privateCollection: WeakMap<object, unknown> | WeakSet<object>,
) {
if (privateCollection.has(obj)) {
throw new TypeError(
"Cannot initialize the same private elements twice on an object",
);
}
}
View on GitHub (pinned to 06b6eae39d)
Solutions
- Do not manually re-invoke the constructor or field initializer on an existing instance — use `new` or Reflect.construct once.
- Audit mixin/inheritance patterns that copy private-field setup; ensure each instance is initialized exactly once.
- Make sure only one compiled copy of the class is loaded (dedupe the package) so two initializer closures do not run on the same object.
- If the error appears after a Babel upgrade, check for duplicate transpilation (build pipeline transpiling twice).
Example fix
// before initPrivateFields(obj); // first call initPrivateFields(obj); // second call -> throws // after initPrivateFields(obj); // call exactly once, ideally inside the constructor
Defensive patterns
Strategy: validation
Validate before calling
// Use a WeakSet to ensure single initialization
const inited = new WeakSet();
function initOnce(obj) {
if (inited.has(obj)) throw new TypeError('already initialized');
inited.add(obj); /* ... */
} Prevention
- Initialize each instance exactly once, inside the constructor.
- Dedupe the compiled class package so two init closures do not run on one object.
- Avoid mixins that re-apply private-field setup.
When it happens
Trigger: At runtime, the private-field initialization code runs twice against the same instance. This usually happens when a constructor (or field setup) is invoked twice on one object — e.g. manually re-running class field initialization, double inheritance of the same private collection, or a transpilation bug producing duplicate init calls. The `privateCollection.has(obj)` check at checkPrivateRedeclaration.ts:7 fires.
Common situations: Calling a transpiled constructor's internal initializer twice (Reflect.construct misuse, manual constructor chaining); a mixin pattern that re-applies private fields; running two different transpiled versions of the same class against one object; corrupt hand-written ES5 shim of a class with private members.
Related errors
- Private element is not present on this object
- Attempted to access private element on non-instance
- Cannot enable both the "privateFieldsAsProperties" and "priv
- Cannot call a class as a function
- this hasn't been initialised - super() hasn't been called
AI-assisted analysis of babel/babel@06b6eae39d (2026-08-03).
Data as JSON: /data/errors/3fc3363514304ef6.json.
Report an issue: GitHub.