Dagre Graph Layout (Legacy)
Dagre is a JavaScript library designed for laying out directed graphs programmatically on the client-side. The package `dagre` at version 0.8.5, which is the latest available on the public npm registry under this name, was last published over six years ago and is effectively abandoned. While the GitHub repository shows recent activity and releases (v2.x, v3.x), these are published under the `@dagrejs/dagre` scoped package. Users attempting to install `dagre` directly will receive the unmaintained 0.8.5 version. This library's core functionality relies on `graphlib` for graph data structures, and it is often paired with rendering libraries like D3 for visualization. Its primary purpose is to automatically compute optimal node and edge positions in directed graphs, differentiating it from manual layout tools.
Common errors
-
TypeError: dagre.layout is not a function
cause Dagre was not correctly imported or loaded, or `graphlib` was not installed/imported, leading to `dagre` being `undefined` or not having the expected methods.fixEnsure `const dagre = require('dagre');` is used for CommonJS environments, or `dagre` is available globally if using a script tag. Also ensure `graphlib` is installed and its `Graph` class is used to create the graph object passed to `dagre.layout`. -
Error: The "dagre" utility is a mandatory dependency.
cause This error often occurs when another library (like JointJS) explicitly checks for the global or module availability of `dagre` and cannot find it.fixVerify that `dagre` is properly installed (`npm install dagre`) and imported/required in your module system. For older `dagre` versions, ensure it's available in the global scope if your integrating library expects it that way.
Warnings
- breaking The `dagre` package on npm (version 0.8.5) is effectively abandoned. Active development, including ESM support, TypeScript rewrite, and new features (versions 2.x and 3.x), has moved to the `@dagrejs/dagre` scoped package. Installing `dagre` will fetch the old, unmaintained version.
- gotcha Version `0.8.5` of `dagre` does not support ES Modules (ESM) syntax. Attempts to use `import dagre from 'dagre'` will result in errors in modern JavaScript environments.
- gotcha Dagre requires the `graphlib` package for its graph data structures. It does not provide its own. Failing to install and use `graphlib` alongside `dagre` will lead to runtime errors when trying to create graphs.
- deprecated Loading `dagre` via a `<script>` tag that exposes a global `dagre` object is a legacy pattern and is not recommended for new projects. This method lacks dependency management and proper module encapsulation.
Install
-
npm install dagre -
yarn add dagre -
pnpm add dagre
Imports
- dagre
import dagre from 'dagre';
const dagre = require('dagre'); - layout
import { layout } from 'dagre';const dagre = require('dagre'); dagre.layout(graph); - Graph
import { Graph } from 'dagre';const graphlib = require('graphlib'); const g = new graphlib.Graph();
Quickstart
const dagre = require('dagre');
const graphlib = require('graphlib');
// Create a new directed graph
const g = new graphlib.Graph().setGraph({
rankdir: 'LR' // Layout direction: Left to Right
}).setDefaultNodeLabel(() => ({})).setDefaultEdgeLabel(() => ({}));
// Add nodes to the graph
g.setNode('A', { label: 'Node A', width: 60, height: 30 });
g.setNode('B', { label: 'Node B', width: 60, height: 30 });
g.setNode('C', { label: 'Node C', width: 60, height: 30 });
g.setNode('D', { label: 'Node D', width: 60, height: 30 });
// Add edges to the graph
g.setEdge('A', 'B', { label: 'AB' });
g.setEdge('B', 'C', { label: 'BC' });
g.setEdge('A', 'D', { label: 'AD' });
g.setEdge('C', 'D', { label: 'CD' });
// Run the layout algorithm
dagre.layout(g);
// Log node positions
g.nodes().forEach(v => {
const node = g.node(v);
console.log(`Node ${v}: x=${node.x}, y=${node.y}`);
});
// Log edge bend points (if any)
g.edges().forEach(e => {
const edge = g.edge(e);
if (edge.points && edge.points.length > 0) {
console.log(`Edge ${e.v}-${e.w} points:`, edge.points);
}
});