mermaid-js/mermaid · error · UnknownDiagramError

Diagram ${type} not found.

Error message

Diagram ${type} not found.

What it means

Thrown by Diagram.fromText after detectType returns a key but getDiagram cannot find it in the registry and getDiagramLoader returns no lazy loader for that type. It is an UnknownDiagramError, meaning the detector recognised the syntax but no implementation (built-in or lazy-loaded) is available to render it.

Source

Thrown at packages/mermaid/src/Diagram.ts:26

// eslint-disable-next-line @typescript-eslint/no-redundant-type-constituents
export type ParseErrorFunction = (err: string | DetailedError | unknown, hash?: any) => void;

/**
 * An object representing a parsed mermaid diagram definition.
 * @privateRemarks This is exported as part of the public mermaidAPI.
 */
export class Diagram {
  public static async fromText(text: string, metadata: Pick<DiagramMetadata, 'title'> = {}) {
    const config = configApi.getConfig();
    const type = detectType(text, config);
    text = encodeEntities(text) + '\n';
    try {
      getDiagram(type);
    } catch {
      const loader = getDiagramLoader(type);
      if (!loader) {
        throw new UnknownDiagramError(`Diagram ${type} not found.`);
      }
      // Diagram not available, loading it.
      // new diagram will try getDiagram again and if fails then it is a valid throw
      const { id, diagram } = await loader();
      registerDiagram(id, diagram);
    }
    const { db, parser, renderer, init } = getDiagram(type);
    if (parser.parser) {
      // The parser.parser.yy is only present in JISON parsers. So, we'll only set if required.
      parser.parser.yy = db;
    }
    db.clear?.();
    init?.(config);
    // This block was added for legacy compatibility. Use frontmatter instead of adding more special cases.
    if (metadata.title) {
      db.setDiagramTitle?.(metadata.title);
    }
    await parser.parse(text);

View on GitHub (pinned to d93e9c88c0)

Solutions

  1. Register the missing diagram: import and registerDiagram(...) or include the external diagram bundle.
  2. Switch to the full mermaid build that ships all built-in diagrams.
  3. Verify the detected type matches a registered id; if a custom detector returns an unknown key, register its implementation.
  4. Catch UnknownDiagramError and surface a user-facing 'unsupported diagram' message.

Example fix

// before
const d = await Diagram.fromText(text);

// after
import { registerDiagram } from 'mermaid';
await registerMyDiagram(); // registers id + loader
const d = await Diagram.fromText(text);
Defensive patterns

Strategy: try-catch

Validate before calling

import { detectType, getDiagramLoader } from 'mermaid';

const type = detectType(text, config);
if (!getDiagramLoader(type)) {
  throw new Error(`No loader registered for diagram type '${type}'`);
}

Type guard

function hasLoader(type: string): boolean {
  return !!getDiagramLoader(type);
}

Try / catch

import { UnknownDiagramError } from 'mermaid';
try {
  const d = await Diagram.fromText(text);
} catch (e) {
  if (e instanceof UnknownDiagramError) {
    // surface 'unsupported diagram' to the user
  } else throw e;
}

Prevention

When it happens

Trigger: Text matches a detector for a diagram type whose package was never registered (e.g. an external diagram not bundled in the build), or a custom detector returns an unregistered key.

Common situations: Using a slimmed-down mermaid build that omits a diagram module, a version mismatch where a detector exists but its loader was removed, or registering a detector without its corresponding diagram.

Related errors


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