{"id":14520,"library":"dagre","title":"Dagre Graph Layout (Legacy)","description":"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.","status":"abandoned","version":"0.8.5","language":"javascript","source_language":"en","source_url":"https://github.com/dagrejs/dagre","tags":["javascript","graph","layout"],"install":[{"cmd":"npm install dagre","lang":"bash","label":"npm"},{"cmd":"yarn add dagre","lang":"bash","label":"yarn"},{"cmd":"pnpm add dagre","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Provides the fundamental graph data structure (Graph) that Dagre operates on. Dagre computes layout for Graphlib instances.","package":"graphlib","optional":false}],"imports":[{"note":"For `dagre@0.8.5`, CommonJS `require` is the standard module import. ESM `import` is not supported in this version.","wrong":"import dagre from 'dagre';","symbol":"dagre","correct":"const dagre = require('dagre');"},{"note":"The primary layout function is a method on the `dagre` object; it is not a named export in version 0.8.5.","wrong":"import { layout } from 'dagre';","symbol":"layout","correct":"const dagre = require('dagre');\ndagre.layout(graph);"},{"note":"Dagre operates on `graphlib.Graph` instances. `graphlib` must be imported separately.","wrong":"import { Graph } from 'dagre';","symbol":"Graph","correct":"const graphlib = require('graphlib');\nconst g = new graphlib.Graph();"}],"quickstart":{"code":"const dagre = require('dagre');\nconst graphlib = require('graphlib');\n\n// Create a new directed graph\nconst g = new graphlib.Graph().setGraph({\n  rankdir: 'LR' // Layout direction: Left to Right\n}).setDefaultNodeLabel(() => ({})).setDefaultEdgeLabel(() => ({}));\n\n// Add nodes to the graph\ng.setNode('A', { label: 'Node A', width: 60, height: 30 });\ng.setNode('B', { label: 'Node B', width: 60, height: 30 });\ng.setNode('C', { label: 'Node C', width: 60, height: 30 });\ng.setNode('D', { label: 'Node D', width: 60, height: 30 });\n\n// Add edges to the graph\ng.setEdge('A', 'B', { label: 'AB' });\ng.setEdge('B', 'C', { label: 'BC' });\ng.setEdge('A', 'D', { label: 'AD' });\ng.setEdge('C', 'D', { label: 'CD' });\n\n// Run the layout algorithm\ndagre.layout(g);\n\n// Log node positions\ng.nodes().forEach(v => {\n  const node = g.node(v);\n  console.log(`Node ${v}: x=${node.x}, y=${node.y}`);\n});\n\n// Log edge bend points (if any)\ng.edges().forEach(e => {\n  const edge = g.edge(e);\n  if (edge.points && edge.points.length > 0) {\n    console.log(`Edge ${e.v}-${e.w} points:`, edge.points);\n  }\n});","lang":"javascript","description":"This example demonstrates how to create a graph using `graphlib`, add nodes and edges, apply the `dagre` layout, and then retrieve the calculated positions for nodes and bend points for edges. It uses CommonJS `require` for compatibility with `dagre@0.8.5`."},"warnings":[{"fix":"For actively maintained and modern features, install `@dagrejs/dagre` instead: `npm install @dagrejs/dagre`.","message":"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.","severity":"breaking","affected_versions":">=0.8.5"},{"fix":"Use CommonJS `const dagre = require('dagre');` for module loading, or switch to the `@dagrejs/dagre` package for ESM support.","message":"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.","severity":"gotcha","affected_versions":"0.1.0 - 0.8.5"},{"fix":"Ensure `graphlib` is installed (`npm install graphlib`) and imported/required separately to create graph objects (e.g., `new graphlib.Graph()`).","message":"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.","severity":"gotcha","affected_versions":">=0.1.0"},{"fix":"Use a module loader (e.g., CommonJS `require` for `dagre@0.8.5`) or migrate to `@dagrejs/dagre` for modern ESM approaches.","message":"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.","severity":"deprecated","affected_versions":">=0.1.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Ensure `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`.","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.","error":"TypeError: dagre.layout is not a function"},{"fix":"Verify 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.","cause":"This error often occurs when another library (like JointJS) explicitly checks for the global or module availability of `dagre` and cannot find it.","error":"Error: The \"dagre\" utility is a mandatory dependency."}],"ecosystem":"npm"}