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

  1. Do not manually re-invoke the constructor or field initializer on an existing instance — use `new` or Reflect.construct once.
  2. Audit mixin/inheritance patterns that copy private-field setup; ensure each instance is initialized exactly once.
  3. 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.
  4. 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

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


AI-assisted analysis of babel/babel@06b6eae39d (2026-08-03). Data as JSON: /data/errors/3fc3363514304ef6.json. Report an issue: GitHub.