Skip to main content
Mermaid needs to be initialized before it can render diagrams. The initialization process sets up the rendering engine, applies configuration, and prepares Mermaid to find and process diagram code.

Quick start

The simplest way to initialize Mermaid is to call initialize() before rendering:
With startOnLoad: true, Mermaid automatically finds and renders all elements with the class mermaid when the page loads.

Initialization methods

The easiest method for static pages:
Mermaid listens for the load event and automatically processes all .mermaid elements.

Configuration options

The initialize() function accepts a configuration object. Here are the most commonly used options:

Basic options

Security levels

  • strict (default): Prevents potentially dangerous features. Tags in text are encoded, click functionality is disabled.
  • loose: Allows more features but can be a security risk. Enables click events and allows some HTML.
  • sandbox: Renders diagrams in a sandboxed iframe for maximum security.

Theme configuration

See the theming guide for more details.

The run() function

The run() function is the modern way to render diagrams:

Examples

The render() function

For programmatic diagram generation without DOM elements:
The render() function returns a Promise that resolves with the SVG string and optional bind functions. Multiple calls are automatically queued and executed serially.

Parsing and validation

Use the parse() function to validate diagram syntax without rendering:

Deprecated: init() function

The init() function is deprecated. Use initialize() and run() instead.
Old pattern:
New pattern:

Framework integration patterns

Best practices

  1. Initialize once - Call initialize() only once, typically at application startup
  2. Use run() for SPAs - Set startOnLoad: false and manually call run() when needed
  3. Handle errors - Always wrap render calls in try-catch or use suppressErrors
  4. Avoid re-initialization - Don’t call initialize() multiple times; use updateSiteConfig() instead
  5. Process diagrams selectively - Use the nodes option for better performance with many diagrams

Next steps

Configuration

Explore detailed configuration options

Theming

Customize diagram appearance

API reference

Complete API documentation

Deployment

Learn about different deployment methods