mermaid-js/mermaid · error

The junction [${id}] cannot be placed within itself

Error message

The junction [${id}] cannot be placed within itself

What it means

Thrown by addJunction when its parent ('in') equals its own id, mirroring the self-containment guard on services. A junction cannot be its own container, so the db rejects id === parent before registration.

Source

Thrown at packages/mermaid/src/diagrams/architecture/architectureDb.ts:126

      title,
      edges: [],
      in: parent,
    });
  }

  public getServices(): ArchitectureService[] {
    return [...this.nodes.values()].filter(isArchitectureService);
  }

  public addJunction({ id, in: parent }: Omit<ArchitectureJunction, 'edges'>): void {
    if (this.registeredIds.has(id)) {
      throw new Error(
        `The junction id [${id}] is already in use by another ${this.registeredIds.get(id)}`
      );
    }
    if (parent !== undefined) {
      if (id === parent) {
        throw new Error(`The junction [${id}] cannot be placed within itself`);
      }
      if (!this.registeredIds.has(parent)) {
        throw new Error(
          `The junction [${id}]'s parent does not exist. Please make sure the parent is created before this junction`
        );
      }
      if (this.registeredIds.get(parent) === 'node') {
        throw new Error(`The junction [${id}]'s parent is not a group`);
      }
    }

    this.registeredIds.set(id, 'node');

    this.nodes.set(id, {
      id,
      type: 'junction',
      edges: [],
      in: parent,

View on GitHub (pinned to d93e9c88c0)

Solutions

  1. Ensure parent id differs from the junction id.
  2. Drop the 'in' field for a top-level junction.
  3. Validate id !== parent before addJunction.

Example fix

// before
db.addJunction({ id: 'j', in: 'j' });

// after
db.addJunction({ id: 'j' });
Defensive patterns

Strategy: validation

Validate before calling

if (id === parent) {
  throw new Error(`Junction '${id}' cannot be its own parent`);
}
db.addJunction({ id, in: parent });

Prevention

When it happens

Trigger: addJunction({ id: 'X', in: 'X' }); or generated layout hint that lists a junction as its own parent.

Common situations: Programmatic spec builder assigns the same id to a junction and its parent slot, or a templating copy bug.

Related errors


AI-assisted analysis of mermaid-js/mermaid@d93e9c88c0 (2026-08-12). Data as JSON: /api/errors/74caa83ff7343e72. Report an issue: GitHub.