dotparser
dotparser is a JavaScript library designed to parse GraphViz dot file format into an Abstract Syntax Tree (AST). It provides a language-agnostic AST representation, making it suitable for graph library authors who need to transform `.dot` files into their own specific graph data structures without being tied to a particular graph visualization or manipulation library. The current stable version is 1.1.1, last published approximately four years ago as of early 2026. While widely downloaded, its development appears to be in maintenance mode, with a successor project, `@ts-graphviz/parser`, offering more modern features and active development. A key differentiator is its focus purely on AST generation, capable of parsing the standard Graphviz test suite, providing a robust foundation for building custom graph tooling.
Common errors
-
SyntaxError: Expected "graph", "digraph", "subgraph", "node", "edge", "strict", "ID", "port", "compass_port" or "attribute" but "..." found.
cause The input string provided to the `parse` function is not valid GraphViz DOT syntax.fixReview the DOT string for syntax errors. GraphViz has a strict grammar. Use online validators or refer to the GraphViz DOT language specification to ensure correctness.
Warnings
- gotcha The `dotparser` package has not received updates for approximately four years as of early 2026. While still functional and widely used, newer GraphViz features or bug fixes might not be supported. Consider more actively maintained alternatives for new projects.
- gotcha When importing `dotparser` in an ES Module (ESM) environment, direct default imports (`import parse from 'dotparser';`) might require the `esModuleInterop` compiler option set to `true` in TypeScript configurations for correct interoperability with its CommonJS export. Without it, you might need to use `import * as dotparser from 'dotparser'; const parse = dotparser.default || dotparser;` or similar patterns in plain JavaScript.
Install
-
npm install dotparser -
yarn add dotparser -
pnpm add dotparser
Imports
- parse
import { parse } from 'dotparser';import parse from 'dotparser';
- parse (CommonJS)
const parse = require('dotparser'); - Type definitions
import { GraphAST } from 'dotparser';import type { GraphAST } from 'dotparser';
Quickstart
import parse from 'dotparser';
const dotString = `
digraph G {
node [shape=box];
a -> b;
b -> c [label="edge 2"];
subgraph cluster_0 {
label = "Cluster 0";
d -> e;
}
}
`;
try {
const ast = parse(dotString);
console.log('Successfully parsed DOT string:');
console.log(JSON.stringify(ast, null, 2));
// Example of accessing elements: The AST is an array of top-level statements
if (ast.length > 0 && ast[0].type === 'graph') {
console.log(`Graph ID: ${ast[0].id}`);
console.log(`Number of children in graph: ${ast[0].children.length}`);
}
} catch (error) {
console.error('Error parsing DOT string:', error.message);
}