mermaid-js/mermaid · error

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

Error message

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

What it means

Thrown by ArchitectureDB.addGroup when a group is declared with its `in` (parent) field equal to its own `id`. Architecture groups form a strict containment tree, so a group cannot be its own ancestor. The check fires before the group is registered, so no partial state is left behind.

Source

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

  }

  public getNodes(): ArchitectureNode[] {
    return [...this.nodes.values()];
  }

  public getNode(id: string): ArchitectureNode | null {
    return this.nodes.get(id) ?? null;
  }

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

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

    this.groups.set(id, {
      id,
      icon,
      title,
      in: parent,

View on GitHub (pinned to d93e9c88c0)

Solutions

  1. Remove the `in` field entirely if the group should be top-level.
  2. Set `in` to the id of a different, already-declared group.

Example fix

// before
db.addGroup({ id: 'api', in: 'api' });
// after
db.addGroup({ id: 'api' });
//   or, nest under an existing group
db.addGroup({ id: 'backend' });
db.addGroup({ id: 'api', in: 'backend' });
Defensive patterns

Strategy: validation

Validate before calling

function addGroupSafe(db, g) {
  if (g.in !== undefined && g.in === g.id) {
    throw new Error(`Refusing to add group: 'in' equals 'id' (${g.id})`);
  }
  db.addGroup(g);
}

Type guard

const isSelfParent = (g) => g.in !== undefined && g.in === g.id;

Prevention

When it happens

Trigger: Calling db.addGroup({ id: 'api', in: 'api' }) — the `in` value matches `id`. In DSL terms, writing `group api in api`.

Common situations: Copy-paste of a group block where the parent name was not changed from the id; auto-generated diagrams that derive `in` from the current group's id; refactoring a group id without updating the self-referential `in`.

Related errors


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