Ditch the Drag-and-Drop: A Hands-On Guide to Mermaid JS
Overview
Software diagrams often become a maintenance nightmare. Traditional drag-and-drop tools are frustrating because arrows break when you move boxes, and version controlling an image file is nearly impossible.
Prerequisites
To follow this tutorial, you should have a basic understanding of
Key Libraries & Tools
- Mermaid JS: A JavaScript-based charting and diagramming tool that renders text definitions into visualizations.
- VS Code: The recommended IDE for local development.
- Markdown Preview Mermaid Support: A vital VS Codeextension that enables real-time rendering of Mermaid blocks within Markdown files.
- Mermaid.live: The official web-based editor used for quick prototyping and exporting diagrams to PNG or SVG formats.
Code Walkthrough
1. Modeling Logic with Flowcharts
Flowcharts are perfect for mapping user interactions or complex conditional logic. You define the direction (Top-Bottom or Left-Right) and then link nodes using arrows.
flowchart TD
A[Start] --> B{Existing User?}
B -- No --> C[Enter Name]
B -- Yes --> D[Send Magic Link]
C --> D
In this snippet, TD sets the orientation. The square brackets [] create standard blocks, while curly braces {} produce diamond decision nodes. Labels placed between characters (like -- No -->) attach text directly to the transition lines.
2. Visualizing Interaction with Sequence Diagrams
When you need to show how different services—like a client, an
sequence_diagram
autonumber
participant Client
participant Server
Client->>Server: Request Resource
activate Server
Server-->>Client: Return Data
deactivate Server
The autonumber keyword is a lifesaver for documentation, as it allows you to refer to specific steps in your written text. The activate and deactivate commands create visual focus on which component is currently processing a request.
3. Structural Mapping with Class Diagrams
Class diagrams help visualize the blueprint of your system. You can define members, methods, and visibility (using + for public, - for private, and # for protected).
classDiagram
class PaymentProcessor {
<<interface>>
-String apiKey
+process(Order order)
}
class Stripe {
+process(Order order)
}
PaymentProcessor <|-- Stripe
Here, <<interface>> provides metadata about the class type, and <|-- represents inheritance. Mermaid also supports aggregation (o--) and composition (*--) to show how objects relate to one another.
Syntax Notes
Mermaid syntax is highly declarative. You don't tell the tool where to draw a line; you tell it what the relationship is. Notable patterns include:
- Node IDs vs. Labels: Using
S[Start]allows you to reference the node asSin your code while displaying "Start" in the diagram. - Styling Nodes: Different brackets change the shape (e.g.,
([Text])for rounded corners or[[Text]]for subroutines). - Directionality: Common codes include
LR(Left to Right),RL(Right to Left),BT(Bottom to Top), andTD(Top Down).
Practical Examples
- Onboarding Documentation: Use flowcharts to show new developers how data travels through your microservices.
- API Documentation: Include sequence diagrams in your README files to show exactly how third-party developers should call your endpoints.
- Database Design: Utilize Entity-Relationship (ER) diagrams to map out your SQLschema before writing a single migration script.
Tips & Gotchas
- Exporting Constraints: While VS Codeis great for live previews, exporting to PDF can be tricky. Use Mermaid.live when you need high-quality image exports for slide decks or reports.
- Layout Limitations: You lose granular control over node placement. If your diagram looks cluttered, try breaking it into smaller, more modular sub-diagrams rather than fighting the layout engine.
- Version Control: Always keep your Mermaid code in your
.mdfiles. This allows teammates to see exactly what changed in a diagram during a code review by looking at the text diff.

Fancy watching it?
Watch the full video and context