ts-graphviz
ts-graphviz is a TypeScript-first library designed to programmatically construct and render Graphviz DOT language graphs. It provides a type-safe API for defining complex graph structures, including nodes, edges, and subgraphs, with full attribute support. The current stable version is 3.0.7, which is part of a monorepo that sees regular patch and minor releases across its constituent packages like `@ts-graphviz/core`, `@ts-graphviz/react`, and `@ts-graphviz/adapter`. This library differentiates itself by leveraging TypeScript's strong typing to prevent common Graphviz syntax errors at compile-time and offering a clean, object-oriented API for graph construction, making it suitable for modern Node.js and browser environments, especially when integrated with frameworks like React.
Common errors
-
Error: spawn dot ENOENT
cause The Graphviz `dot` command-line tool is not found in the system's PATH, which is required by the `render` function.fixInstall the Graphviz CLI tools on your operating system (e.g., `brew install graphviz` on macOS, `sudo apt-get install graphviz` on Linux) and ensure it's accessible in your PATH. -
TypeError: (0 , ts_graphviz_1.digraph) is not a function
cause Attempting to use CommonJS `require()` syntax or an incorrect import path for `ts-graphviz`, which is primarily an ESM package.fixEnsure you are using ES module `import` syntax (`import { digraph } from 'ts-graphviz';`) and that your project is configured for ES modules (e.g., `"type": "module"` in `package.json`). -
TS2307: Cannot find module 'ts-graphviz' or its corresponding type declarations.
cause The `ts-graphviz` package is not installed, or TypeScript cannot resolve its types due to incorrect `tsconfig.json` setup or a missing `@types/ts-graphviz` (though this package ships its own types).fixInstall the package (`npm install ts-graphviz` or `yarn add ts-graphviz`). Verify your `tsconfig.json` includes `"moduleResolution": "Bundler"` or `"NodeNext"` and that `"skipLibCheck": true` is not hiding issues.
Warnings
- breaking ts-graphviz requires Node.js version 20 or higher. Running on older Node.js versions may lead to unexpected errors or incompatibility issues.
- gotcha The `render` function relies on the external Graphviz command-line tools (e.g., `dot`) being installed and accessible in your system's PATH. If not found, `render` will throw an error.
- breaking Version 3.0.6 introduced a security fix that adds null byte sanitization to prevent Graphviz parsing errors and potential DOT injection. This changes how null bytes in labels/attributes are handled.
- gotcha When using `@ts-graphviz/react`, JSX type definitions for HTML-like `dot:` elements (e.g., `dot:table`, `dot:tr`) are automatically augmented upon import. If these types are not recognized, ensure `@ts-graphviz/react` is correctly imported and your TypeScript configuration is set up for JSX.
Install
-
npm install ts-graphviz -
yarn add ts-graphviz -
pnpm add ts-graphviz
Imports
- digraph
const { digraph } = require('ts-graphviz');import { digraph } from 'ts-graphviz'; - node
import node from 'ts-graphviz/node';
import { node } from 'ts-graphviz'; - render
import { render } from '@ts-graphviz/adapter';import { render } from 'ts-graphviz'; - toDot
import { toDot } from 'ts-graphviz';
Quickstart
import { digraph, node, edge, toDot, render } from 'ts-graphviz';
import { writeFile } from 'fs/promises';
async function generateGraphImage() {
const G = digraph('MyGraph', (g) => {
const start = node('start', { shape: 'Mdiamond', label: 'Start' });
const processA = node('processA', { label: 'Process A' });
const processB = node('processB', { label: 'Process B' });
const end = node('end', { shape: 'Msquare', label: 'End' });
edge([start, processA], { label: 'Init' });
edge([processA, processB], { label: 'Data Transfer' });
edge([processB, end], { label: 'Completion', color: 'green' });
g.subgraph('cluster_steps', (c) => {
c.label = 'Workflow Steps';
c.style = 'filled';
c.color = 'lightgrey';
c.addNode(processA, processB);
});
g.addNode(start, end);
});
const dotString = toDot(G);
console.log('Generated DOT string:\n', dotString);
try {
// Ensure Graphviz CLI 'dot' is installed and available in your system's PATH.
// e.g., on macOS: `brew install graphviz`
// e.g., on Ubuntu: `sudo apt-get install graphviz`
const imageBuffer = await render(G, { format: 'png' });
await writeFile('workflow_graph.png', imageBuffer);
console.log('Graph image successfully generated at workflow_graph.png');
} catch (error) {
console.error('Error rendering graph. Is Graphviz CLI installed and in PATH?', error);
throw error;
}
}
generateGraphImage();