Headless Mermaid Rendering (`@m2d/mermaid-node`)
Updated 20 Oct 2026
Server-side Mermaid rendering with beautiful-mermaid, transparent backgrounds, and sharp color inlining.
@m2d/mermaid-node renders Mermaid diagrams completely server-side in Node.js without requiring headless browsers (Puppeteer/Chromium), JSDOM, or canvas emulators.
Why Headless beautiful-mermaid?
Traditional server-side Mermaid rendering requires spawning a full headless Chromium browser process, consuming hundreds of megabytes of RAM and adding significant latency.
@m2d/mermaid-node uses beautiful-mermaid, which runs a pure TypeScript ELK.js layout engine:
- Zero Headless Chrome: Runs anywhere Node.js runs (AWS Lambda, Cloud Run, CLI tools, Next.js API routes).
- Sub-10ms Compilation: Renders diagrams in milliseconds instead of seconds.
- Micro Bundle: Zero native browser dependencies.
Sharp & librsvg Color Compatibility
beautiful-mermaid uses a modern CSS variable and color-mix() architecture (e.g. fill="var(--_node-fill)"). However, Node's standard SVG rasterizer (librsvg via sharp) does not evaluate CSS custom properties, which historically caused all rendered diagram shapes and text to appear solid black.
@m2d/mermaid-node automatically:
- Inlines and resolves all CSS variables and sRGB
color-mix()calculations into explicit hex colors directly on SVG attributes before returning the SVG string. - Preserves a transparent canvas background by default (
transparent: true), allowing diagrams to blend seamlessly onto page backgrounds. - Auto-normalizes sequence diagrams with multi-word actor names (e.g.
Client->>Server API: ...) by injectingparticipant <id> as <name>declarations.
Supported Diagram Types
beautiful-mermaid natively supports 6 core diagram types:
- Flowcharts / Graphs (
graph TD,flowchart LR) - Sequence Diagrams (
sequenceDiagram) - Class Diagrams (
classDiagram) - State Diagrams (
stateDiagram-v2) - Entity Relationship Diagrams (
erDiagram) - XY Charts (
xychart-beta)
Diagram types outside this list (e.g. Gantt, Pie charts) or diagrams with unrecoverable syntax errors cleanly trigger @m2d/image-node's fallback policy, rendering as collapsible H6 headings + formatted Word code blocks.
Frequently Asked Questions (FAQ)
Why not use a headless browser (Puppeteer / Playwright / Chromium) in Node?
The Rule of Thumb: If you are already running a headless browser in your backend, run the entire conversion inside that browser using @m2d/mermaid and @m2d/md2docx instead!
- Overhead & Infrastructure Burden: Spawning headless Chromium requires hundreds of megabytes of RAM, native C++ OS dependencies (which frequently break in minimal Alpine/Docker containers and AWS Lambda), and seconds of warmup latency.
- The Architectural Contradiction: If a server environment already supports headless Chrome, there is no need for a hybrid Node diagram plugin—the entire document pipeline can execute natively in the browser with full DOM, Canvas, and official
mermaid.jssupport. - The Sweet Spot of
@m2d/mermaid-node: Designed for high-throughput, edge, serverless, and CLI environments that demand pure TypeScript execution, sub-10ms rendering, and zero binary dependencies.
What are the limitations of beautiful-mermaid?
beautiful-mermaid is an ultra-fast, zero-DOM TypeScript layout engine using ELK.js rather than the full D3/DOM-dependent mermaid.js bundle:
- Supported (6 Core Types): Flowcharts, Sequence Diagrams, Class Diagrams, State Diagrams, ER Diagrams, and XY Charts.
- Unsupported: Gantt charts, Pie charts, Git graphs, Mindmaps, Timelines, and Quadrant charts.
- Graceful Fallback: Unsupported or syntax-errored diagrams do not crash document generation;
@m2d/image-nodecaptures the error and embeds the source code as a formatted Word code block with an informative heading.