File: README.md
mermaid-cli
This is a command-line interface (CLI) for mermaid. It takes a mermaid definition file as input and generates an SVG/PNG/PDF file as output.
Installation
npm install @mermaid-js/mermaid-cliUsage
Convert Mermaid mmd Diagram File To SVG
mmdc -i input.mmd -o output.svgNote
See Alternative installations if you don't want to install the package globally.
Locate how to call the mmdc executable for your preferred method
i.e. Docker, Yarn, NPM, local install, etc.
Examples
Create A PNG With A Dark Theme And Transparent Background
mmdc -i input.mmd -o output.png -t dark -b transparentAnimating an SVG file with custom CSS
The --cssFile option can be used to inline some custom CSS.
Please see ./test-positive/flowchart1.css for an example of a CSS file that has animations.
Warning: If you want to override mermaid's themeCSS, we recommend instead adding {"themeCSS": "..."}) to your mermaid --configFile. You may also need to use !important to override mermaid's themeCSS.
Warning: Inline CSS files may be blocked by your browser, depending on the HTTP Content-Security-Policy header of the website that hosts your SVG.
mmdc --input test-positive/flowchart1.mmd --cssFile test-positive/flowchart1.css -o docs/animated-flowchart.svgExample output: docs/animated-flowchart.svg
Transform a markdown file with mermaid diagrams
mmdc -i readme.template.md -o readme.mdThis command transforms a markdown file itself. The mermaid-cli will find the mermaid diagrams, create SVG files from them and refer to those in the markdown output.
This:
### Some markdown
```mermaid
graph
[....]
```
### Some more markdown
```mermaid
sequenceDiagram
[....]
```
### Mermaid with custom title/desc
```mermaid
graph
accTitle: My title here
accDescr: My description here
A-->B
```Becomes:
### Some markdown
### Some more markdown
### Mermaid with custom title/descPiping from stdin
You can easily pipe input from stdin. This example shows how to use a heredoc to
send a diagram as stdin to mermaid-cli (mmdc).
cat << EOF | mmdc --input -
graph TD
A[Client] --> B[Load Balancer]
EOFSee All Available Options
mmdc -hAlternative installations
Use Docker/Podman:
docker pull minlag/mermaid-clior pull from Github Container Registry
docker pull ghcr.io/mermaid-js/mermaid-cli/mermaid-clior e.g. version 8.8.0
docker pull minlag/mermaid-cli:8.8.0The container looks for input files in /data. So for example, if you have a
diagram defined on your system in /path/to/diagrams/diagram.mmd, you can use
the container to generate an SVG file as follows:
docker run --rm -u `id -u`:`id -g` -v /path/to/diagrams:/data minlag/mermaid-cli -i diagram.mmdOr, if using Podman, instead do:
podman run --userns keep-id --user ${UID} --rm -v /path/to/diagrams:/data:z ghcr.io/mermaid-js/mermaid-cli/mermaid-cli -i diagram.mmdThe key differences in the podman command versus the docker command are:
- The addition of the
--userns keep-idargument. This allows the container to keep the same UID as the current user's UID in the container namespace instead of mapping to a subuid. To learn more, see theuserns=modedocumentation. - The addition of
:zto the end of the volume mapping. This instructs podman to relabel the files in the volume with the SELinux labelcontainer_file_t, which allows processes in the container to access the files. See the "Labeling Volume Mounts" section of podman-run (1) for more info.
In previous version, the input files were mounted in /home/mermaidcli. You can
restore this behaviour with the --workdir option:
docker run [...] --workdir=/home/mermaidcli minlag/mermaid-cli [...]Use Node.JS API
It's possible to call mermaid-cli via a Node.JS API.
Please be aware that the NodeJS API is not covered by semver, as mermaid-cli followsmermaid's versioning.
import { run } from "@mermaid-js/mermaid-cli";
await run(
"input.mmd",
"output.svg", // {optional options},
);Install locally
Some people are having issues
installing this tool globally. Installing it locally is an alternative solution:
npm install @mermaid-js/mermaid-cli
./node_modules/.bin/mmdc -hRun with npx
npx is installed by default with NPM. It
downloads and runs commands at the same time. To use Mermaid CLI with npx, you
need to use the -p flag because the package name is different than the command
it installs (mmdc). npx -p @mermaid-js/mermaid-cli mmdc -h
Install with [brew](https://brew.sh)
Warning
This method of installation is no longer supported.
For more details, see the discussion.
An old version of mermaid-cli can be installed with brew.shbrew install mermaid-cli
Known issues
- Linux sandbox issue
- Docker permission denied issue
- How to setup up mermaid to use already installed chromium?
For contributors
Contributions are welcome. See the contribution guide.
File: docs/already-installed-chromium.md
The text below is taken from Issue #331.
If you run mermaid-cli with the --help flag, there's an option for --puppeteerConfigFile, which is a JSON configuration file for puppeteer.
These are options passed to puppeteer.launch.
The useful options for you are:
executablePathPath to a browser executable to use instead of the bundled Chromium. Note that Puppeteer is only guaranteed to work with the bundled Chromium, so use this setting at your own risk. You may find https://github.com/puppeteer/puppeteer/blob/main/versions.js useful to see which versions of Chrome are compatible with versions of puppeteer.product(in case you want to use firefox instead of chrome)timeoutcan be used to increase/decrease timeout before puppeteer throws an error
You'll probably want a file called puppeteerConfigFile.json with contents:
{
"executablePath": "C:\\path\\to\\your\\chrome.exe"
}Then run mermaid-cli with option mmdc --puppeteerConfigFile puppeteerConfigFile.json.
You might also be able to do the same thing with environment variables by following: https://pptr.dev/#environment-variables.
Btw, if you set PUPPETEER_SKIP_DOWNLOAD=1 as an env variable when running yarn install or npm install, it should skip downloading CHROMIUM too.
File: docs/docker-permission-denied.md
When running the Docker image with Docker or Podman you get the following error:
โบ podman run -it -v "$(pwd)":/data:z minlag/mermaid-cli -i /data/diagram.mmd
node:fs:582
handleErrorFromBinding(ctx);
^
Error: EACCES: permission denied, open '/data/diagram.mmd.svg'
at Object.openSync (node:fs:582:3)
at Object.writeFileSync (node:fs:2143:35)
at /home/mermaidcli/node_modules/@mermaid-js/mermaid-cli/index.bundle.js:169:10
at Generator.next (<anonymous>)
at step (/home/mermaidcli/node_modules/@mermaid-js/mermaid-cli/index.bundle.js:4:191)
at /home/mermaidcli/node_modules/@mermaid-js/mermaid-cli/index.bundle.js:4:361
at processTicksAndRejections (node:internal/process/task_queues:96:5) {
errno: -13,
syscall: 'open',
code: 'EACCES',
path: '/data/diagram.mmd.svg'
}You can solve this issue by using docker -u option with your UID:
docker run -u $UID -it --rm -v "$(pwd)":data minlag/mermaid-cli -i /data/diagram.mmdThe issue is described in issue #140
File: docs/linux-sandbox-issue.md
(node:8281) UnhandledPromiseRejectionWarning: Error: Failed to launch chrome!
[0416/092218.828861:ERROR:zygote_host_impl_linux.cc(88)] Running as root without --no-sandbox is not supported. See https://crbug.com/638180.
(node:8191) UnhandledPromiseRejectionWarning: Error: Failed to launch chrome!
[0416/091938.210735:FATAL:zygote_host_impl_linux.cc(124)] No usable sandbox! Update your kernel or see https://chromium.googlesource.com/chromium/src/+/master/docs/linux_suid_sandbox_development.md for more information on developing with the SUID sandbox. If you want to live dangerously and need an immediate workaround, you can try using --no-sandbox.First and foremost, you should not run as root and you should upgrade your Linux kernel to latest version.
But if you don't want to follow the advice above and just want to disable sandbox, here you go:
Create a puppeteer-config.json file:
{
"args": ["--no-sandbox"]
}And when you invoke mmdc:
mmdc -p puppeteer-config.json ...File: docs/reviewing.md
Reviewing a PR
Running Percy Visual Inspection tests from a PR from a fork
In order to run Percy visual inspection tests, a PR from a fork may have to
be brought into the main repo.
This is because uploading to Percy requires a GitHub Secret token, which cannot
be accessed from forks for security reasons.
After you have reviewed the work, and made sure there is no security exploit
(e.g. no echo "$TOKEN"), then you can run the following to get percy working:
git switch -c "<NAME_OF_YOUR_BRANCH>"
git pull "https://github.com/<NAME_OF_YOUR_USERNAME>/mermaid-cli.git" "<NAME_OF_YOUR_BRANCH>"
# Make sure the commit hash matches the commit hash of the PR you reviewed
git log --max-count=1
git push [email protected]:mermaid-js/mermaid-cli.git "<NAME_OF_YOUR_BRANCH>"After pushing, CI should re-run on that commit, and Percy should show up on the
PR after a few minutes, as long as both branches have the same commit hash.