npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

mermaid-svg-native

v0.1.2

Published

Pure Mathematical Mermaid Rendering To SVG Without Headless Browser

Readme

mermaid-svg-native

Rendering Mermaid to SVG without "Headless Browser" nor Mermaid monkey-patching.

Installation

npm i mermaid-svg-native happy-dom canvas opentype.js

Optional Dependencies

  • happy-dom - For DOM emulation for certain graph types.
  • canvas - For Canvas emulation for certain graph types.
  • opentype.js - For loading and parsing OpenType font files.

Usage

Quick Setup

import { injectMermaidSVGPolyfills, RECOMMENDED_MERMAID_CONFIG } from "mermaid-svg-native"

import OpenSansTTF from "mermaid-svg-native/fonts/open-sans.ttf"
import TrebuchetMsTTF from "mermaid-svg-native/fonts/trebuchet-ms.ttf"

await injectMermaidSVGPolyfills(globalThis, {
  default: OpenSansTTF, 
  "trebuchet ms": TrebuchetMsTTF, 
})

// Must initialize Mermaid after polyfills since it has side-effects.
const { default: mermaid } = await import("mermaid")
mermaid.initialize(RECOMMENDED_MERMAID_CONFIG(globalThis))

const { svg } = await mermaid.render("diagram-id", "graph TD; A-->B;")
console.log(svg)

// import { writeFileSync } from "fs"
// writeFileSync("diagram.svg", svg) // Save SVG to file to view it.

Advanced Setup

import {
  loadOpenTypeFonts,

  injectDOMPolyfill,
  injectDOMCanvasPolyfill,
  injectDOMPointPolyfill,
  injectSvgBBoxPolyfill,
} from "mermaid-svg-native"

// Register fonts for SVG BBox Polyfill.
Object.assign(SvgBBox.fonts, await loadOpenTypeFonts(fontsPathsToLoad))

injectDOMPolyfill(globalThis)
injectDOMCanvasPolyfill(globalThis)
injectDOMPointPolyfill(globalThis)
injectSvgBBoxPolyfill(globalThis)

const { default: mermaid } = await import("mermaid")
mermaid.initialize({
  securityLevel: "loose", // Required to render.
  htmlLabels: false, // Required to switch to SVG text rendering.
  flowchart: { htmlLabels: false },

  // Other configurations to your taste.
  startOnLoad: false,
  wrap: true,
  markdownAutoWrap: true,
  gantt: {
    barGap: 15,
    useWidth: window.innerWidth
  }
})

You must specify securityLevel: "loose" (reference) and htmlLabels: false to enable SVG text rendering in Mermaid.

Custom DOM Polyfill

If you're already using a DOM polyfill like jsdom or happy-dom, you can skip the injectDOMPolyfill step and only inject the necessary polyfills for Canvas, DOMPoint, and SVG BBox.

However, you should make sure that CSS computed styles are supported in your DOM polyfill, as Mermaid relies on them for accurate rendering.

Motivation

Mermaid is a popular diagramming and charting tool that generates SVG diagrams from text-based descriptions. However, rendering Mermaid diagrams in Node.js environments typically requires a headless browser (like Puppeteer) or monkey-patching Mermaid's internal functions to work around the lack of DOM and Canvas APIs.

This library provides polyfills for the necessary DOM and Canvas APIs, allowing Mermaid to render SVG diagrams natively in Node.js without the overhead of a headless browser or the fragility of monkey-patching.

This approach offers several benefits:

  • Performance: Eliminates the need for a headless browser, resulting in faster rendering speed.
  • Resource Efficiency: Reduces memory and CPU usage by avoiding the overhead associated with running a browser instance.
  • Bundle size: Smaller bundle size compared to solutions that require a headless browser.
  • No Browser Dependencies: No more loading heavy browser packages and machine incompatibility issues. You can run it on a very restricted environment.
  • Stability: Avoids monkey-patching, which can break with Mermaid updates, leading to more stable and maintainable code.
  • Simplicity: Provides a straightforward way to render Mermaid diagrams in Node.js environments. Can be easily integrated into various Node.js applications, including server-side rendering, static site generation, and more. Don't require running bash scripts or managing browser dependencies.

Caveats

  • Font Management: Users must provide font files for accurate text measurement and rendering. The library includes default fonts used by Mermaid. Learn more in the Font Management document.
  • SVG Features: While the library aims to support the necessary SVG features for Mermaid diagrams, there may be small issues compared to a full browser environment. Complex diagrams or advanced SVG features may not render perfectly.
  • Line Breaks: Line breaks likely won't be displayed accurately right now, as the library does not yet currently support it, but it's planned for future releases.
  • SVG Only: Only SVG-based text measurement and rendering are supported. HTML labels or other non-SVG rendering methods are not supported since they require HTML layout polyfills - this library covers only SVG measurements.