mermaid-js/mermaid · error · Error

parser.parser?.yy was not a PacketDB. This is due to a bug w

Error message

parser.parser?.yy was not a PacketDB. This is due to a bug within Mermaid, please report this issue at https://github.com/mermaid-js/mermaid/issues.

What it means

Thrown by the packet parser's parse() when parser.parser.yy is not an instance of PacketDB. The yy field (a yacc convention meaning 'your variable') is supposed to be set by mermaid's diagram registration before parse runs, wiring the parser to its db. If that wiring didn't happen — e.g. the diagram type wasn't registered, or a custom integration called parse() directly without setting yy — the db is undefined or wrong-typed and parsing cannot proceed. The message itself directs users to report it as a mermaid bug.

Source

Thrown at packages/mermaid/src/diagrams/packet/parser.ts:100

      bits: rowEnd - block.start,
    },
    {
      start: rowStart,
      end: block.end,
      label: block.label,
      bits: block.end - rowStart,
    },
  ];
};

export const parser: ParserDefinition = {
  // @ts-expect-error - PacketDB is not assignable to DiagramDB
  parser: { yy: undefined },
  parse: async (input: string): Promise<void> => {
    const ast: Packet = await parse('packet', input);
    const db = parser.parser?.yy;
    if (!(db instanceof PacketDB)) {
      throw new Error(
        'parser.parser?.yy was not a PacketDB. This is due to a bug within Mermaid, please report this issue at https://github.com/mermaid-js/mermaid/issues.'
      );
    }
    log.debug(ast);
    populate(ast, db);
  },
};

View on GitHub (pinned to d93e9c88c0)

Solutions

  1. If using mermaid programmatically, call mermaid.parse()/mermaid.render() which handles yy wiring — do not invoke the packet parser directly.
  2. Ensure @mermaid-js/parser and mermaid package versions are aligned (no mismatched installs).
  3. If you must call the parser directly, set parser.parser.yy to a fresh PacketDB instance before parse().
  4. Report upstream if this occurs through the standard mermaid.render() API.

Example fix

// before — direct parser use, yy unset
import { parser } from './packet/parser.js';
await parser.parse(input);  // throws: yy not a PacketDB

// after — let mermaid wire the db
import mermaid from 'mermaid';
await mermaid.parse('packet-beta\n  0-7: Field A');
Defensive patterns

Strategy: type-guard

Validate before calling

import { PacketDB } from './db.js';
import { parser } from './parser.js';

if (!(parser.parser?.yy instanceof PacketDB)) {
  parser.parser.yy = new PacketDB();
}

Type guard

function isPacketDB(db: unknown): db is PacketDB {
  return db instanceof PacketDB;
}

Try / catch

try {
  await mermaid.render('g', diagramText);
} catch (e) {
  if (e instanceof Error && /was not a PacketDB/.test(e.message)) {
    showUserError('Packet diagram failed to initialize its database. Ensure you use mermaid.render() and that mermaid/parser versions match.');
  } else throw e;
}

Prevention

When it happens

Trigger: Calling parser.parse() directly without first assigning parser.parser.yy = new PacketDB(); a mermaid internal where diagram registration for 'packet' failed or was skipped; using a stale parser instance across diagram clears.

Common situations: Custom/programmatic use of mermaid's parser outside the standard mermaid.render() flow; SSR or test setups that import the parser in isolation; version mismatches between @mermaid-js/parser and the mermaid package.

Related errors


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