mermaid-js/mermaid · error · DiagramNotFoundError

Diagram ${name} not found.

Error message

Diagram ${name} not found.

What it means

Thrown by getDiagram(name) when name is not a key in the internal diagrams registry (a DiagramNotFoundError subclass of Error). It is the synchronous lookup counterpart to the lazy-load path; it fires when code asks for a diagram definition by name and that name was never registerDiagram'd.

Source

Thrown at packages/mermaid/src/diagram-api/diagramAPI.ts:76

  diagram.injectUtils?.(
    log,
    setLogLevel,
    getConfig,
    sanitizeText,
    setupGraphViewbox,
    getCommonDb(),
    () => {
      // parseDirective is removed in https://github.com/mermaid-js/mermaid/pull/4759.
      // This is a no-op for legacy support.
    }
  );
};

export const getDiagram = (name: string): DiagramDefinition => {
  if (name in diagrams) {
    return diagrams[name];
  }
  throw new DiagramNotFoundError(name);
};

export class DiagramNotFoundError extends Error {
  constructor(name: string) {
    super(`Diagram ${name} not found.`);
  }
}

View on GitHub (pinned to d93e9c88c0)

Solutions

  1. Ensure the diagram module is imported and registerDiagram(...) ran before getDiagram.
  2. Use the lazy loader path (getDiagramLoader) for external diagrams instead of assuming synchronous availability.
  3. Catch DiagramNotFoundError and trigger the loader, then retry.
  4. Verify the name string exactly matches the registered id (case-sensitive).

Example fix

// before
const { db, renderer } = getDiagram(type);

// after
let def;
try { def = getDiagram(type); }
catch {
  const loader = getDiagramLoader(type);
  if (!loader) throw new Error(`Unsupported: ${type}`);
  const { id, diagram } = await loader();
  registerDiagram(id, diagram);
  def = getDiagram(type);
}
const { db, renderer } = def;
Defensive patterns

Strategy: try-catch

Validate before calling

import { getDiagramLoader, registerDiagram } from 'mermaid';

async function ensureDiagram(name: string) {
  try { return getDiagram(name); }
  catch {
    const loader = getDiagramLoader(name);
    if (!loader) throw new Error(`Unsupported diagram: ${name}`);
    const { id, diagram } = await loader();
    registerDiagram(id, diagram);
    return getDiagram(name);
  }
}

Type guard

function isRegistered(name: string): boolean {
  try { getDiagram(name); return true; } catch { return false; }
}

Try / catch

import { DiagramNotFoundError } from 'mermaid';
try {
  const def = getDiagram(name);
} catch (e) {
  if (e instanceof DiagramNotFoundError) {
    // load and register, then retry
  } else throw e;
}

Prevention

When it happens

Trigger: Calling getDiagram('foo') for an unregistered type; Diagram.fromText reaching the final getDiagram after a failed loader registration left the registry unchanged; calling a diagram db/parser before its module loaded.

Common situations: A tree-shaken build dropped a diagram module, dynamic import failed silently, or a custom integration calls getDiagram before registerDiagram.

Related errors


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