mermaid-js/mermaid · error · Error

Class not found: ${id}

Error message

Class not found: ${id}

What it means

Thrown by ClassDB.lookUpDomId when the sanitized id is not present in the classes map. The method sanitizes the raw id via common.sanitizeText before lookup, so the id used for the miss may differ from what the caller passed (e.g. whitespace or disallowed characters stripped). It is the canonical way to resolve a class's DOM id for rendering, and a miss means no class with that (sanitized) name was ever declared.

Source

Thrown at packages/mermaid/src/diagrams/class/classDb.ts:170

   */
  public setDiagramId(svgElementId: string) {
    this.diagramId = svgElementId;
  }

  /**
   * Function to lookup domId from id in the graph definition.
   * When diagramId is set, returns the prefixed version for DOM uniqueness.
   *
   * @param id - class ID to lookup
   * @public
   */
  public lookUpDomId(_id: string): string {
    const id = common.sanitizeText(_id, getConfig());
    if (this.classes.has(id)) {
      const domId = this.classes.get(id)!.domId;
      return this.diagramId ? `${this.diagramId}-${domId}` : domId;
    }
    throw new Error('Class not found: ' + id);
  }

  public clear() {
    this.relations = [];
    this.classes = new Map<string, ClassNode>();
    this.notes = new Map<string, ClassNote>();
    this.interfaces = [];
    this.functions = [];
    this.functions.push(this.setupToolTips.bind(this));
    this.namespaces = new Map<string, NamespaceNode>();
    this.namespaceCounter = 0;
    this.namespaceStack = [];
    this.diagramId = '';
    this.direction = 'TB';
    commonClear();
  }

  public getClass(id: string): ClassNode {

View on GitHub (pinned to d93e9c88c0)

Solutions

  1. Declare the class (via addClass or the class DSL) before calling lookUpDomId.
  2. Check the spelling and case of the id.
  3. Remember the lookup uses the sanitized form — pass the same raw id form that was used at declaration so sanitization produces a matching key.

Example fix

// before
db.lookUpDomId('Ghost'); // never declared
// after
db.addClass('Ghost');
db.lookUpDomId('Ghost');
Defensive patterns

Strategy: validation

Validate before calling

function lookUpDomIdSafe(db, id) {
  if (!db.getClasses().has(id)) {
    throw new Error(`Class '${id}' not declared; cannot look up domId`);
  }
  return db.lookUpDomId(id);
}

Type guard

const classExists = (db, id) => db.getClasses().has(id);

Prevention

When it happens

Trigger: Calling db.lookUpDomId('Ghost') where 'Ghost' was never added via addClass. Also: calling lookUpDomId on an id whose sanitized form doesn't match any declared class — e.g. passing 'My Class' when the declared id is 'MyClass'.

Common situations: Referencing a class (in a relation, note, or annotation) before it is declared; typos or case mismatches; ids containing characters that sanitizeText strips or collapses, so the caller's string doesn't match the stored key.

Related errors


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