{"id":15116,"library":"graphology","title":"Graphology: JavaScript Graph Library","description":"Graphology is a comprehensive JavaScript library providing a robust and multipurpose Graph object. It implements a unified specification for various graph types, including directed, undirected, multi, and mixed graphs, making it a versatile tool for network analysis and graph theory applications. The library focuses on performance and memory efficiency, particularly when handling complex multigraph structures. The current stable version is 0.26.0, which introduced explicit ESM support and removed some internal dependencies. Releases are made periodically, incorporating performance enhancements, new features like additional degree methods, and refinements to its internal architecture. It distinguishes itself by offering a well-defined and consistent API for graph manipulation.","status":"active","version":"0.26.0","language":"javascript","source_language":"en","source_url":"https://github.com/graphology/graphology","tags":["javascript","graph","graph theory","directed","undirected","network","typescript"],"install":[{"cmd":"npm install graphology","lang":"bash","label":"npm"},{"cmd":"yarn add graphology","lang":"bash","label":"yarn"},{"cmd":"pnpm add graphology","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Provides TypeScript type definitions for graphology. This is a peer dependency, so it must be installed alongside graphology if TypeScript is used.","package":"graphology-types","optional":false}],"imports":[{"note":"Since v0.26.0, graphology fully supports ESM. While CommonJS `require` might still work in some setups, `import` is the recommended and more robust approach. Graph is typically imported as a default export.","wrong":"const Graph = require('graphology');","symbol":"Graph","correct":"import Graph from 'graphology';"},{"note":"For TypeScript projects, you can import the `Graph` type directly using `import type` to avoid bundling issues and ensure type safety.","symbol":"Graph type","correct":"import type Graph from 'graphology';"},{"note":"Unlike some graph libraries, `graphology` typically uses a single `Graph` class and configuration options (e.g., `type: 'directed'`) to create specific graph instances, rather than separate named exports for each graph type.","wrong":"import { DirectedGraph } from 'graphology';","symbol":"Specific graph factory (e.g., DirectedGraph)","correct":"import Graph from 'graphology'; const directedGraph = new Graph({ type: 'directed' });"}],"quickstart":{"code":"import Graph from 'graphology';\n\ninterface NodeAttributes {\n  label: string;\n}\n\ninterface EdgeAttributes {\n  weight: number;\n}\n\n// Create a new directed graph\nconst graph = new Graph<NodeAttributes, EdgeAttributes, any>({\n  type: 'directed',\n  multi: false\n});\n\n// Add nodes with attributes\ngraph.addNode('A', { label: 'Node A' });\ngraph.addNode('B', { label: 'Node B' });\ngraph.addNode('C', { label: 'Node C' });\n\n// Add edges with attributes\ngraph.addEdge('A', 'B', { weight: 1 });\ngraph.addEdge('B', 'C', { weight: 2 });\ngraph.addEdge('C', 'A', { weight: 3 });\n\n// Check graph properties\nconsole.log(`Number of nodes: ${graph.order}`);\nconsole.log(`Number of edges: ${graph.size}`);\n\n// Iterate over nodes and their attributes\nconsole.log('Nodes:');\ngraph.forEachNode((node, attributes) => {\n  console.log(`- ${node} (Label: ${attributes.label})`);\n});\n\n// Iterate over edges and their attributes\nconsole.log('Edges:');\ngraph.forEachEdge((edge, attributes, source, target) => {\n  console.log(`- ${source} -> ${target} (Weight: ${attributes.weight})`);\n});\n\n// Get neighbors of a node\nconst neighborsOfB = graph.neighbors('B');\nconsole.log(`Neighbors of B: ${neighborsOfB.join(', ')}`);\n\n// Check if node exists\nconsole.log(`Does node 'D' exist? ${graph.hasNode('D')}`);","lang":"typescript","description":"This quickstart demonstrates how to create a directed graph, add nodes and edges with custom attributes, and perform basic operations like counting elements and iterating over nodes and edges."},"warnings":[{"fix":"Migrate your import statements to ESM syntax (e.g., `import Graph from 'graphology';`). Ensure your `package.json` has `\"type\": \"module\"` if you are running in Node.js ESM mode, or configure your bundler appropriately.","message":"Graphology v0.26.0 introduced explicit ESM support. Projects using CommonJS `require()` might encounter module resolution errors or unexpected behavior, especially in modern Node.js environments or bundlers. The library primarily targets ESM.","severity":"breaking","affected_versions":">=0.26.0"},{"fix":"If you were using these methods, refactor your graph manipulation logic to explicitly create new graphs with the desired `type` and `multi` options, and copy nodes/edges as needed, or adjust your graph creation strategy.","message":"The undocumented methods `#.upgradeToMixed` and `#.upgradeToMulti` were removed in v0.24.0. Code relying on these internal methods will break.","severity":"breaking","affected_versions":">=0.24.0"},{"fix":"Ensure your target environment supports `Array.from`, or include a polyfill (e.g., `core-js`) if targeting older platforms that do not provide this method natively.","message":"As of v0.26.0, `graphology` no longer shims `Array.from`. This means that environments lacking native `Array.from` support (e.g., very old browsers or Node.js versions) will require a polyfill if `graphology`'s internal operations depend on it.","severity":"breaking","affected_versions":">=0.26.0"},{"fix":"Thoroughly test existing graph manipulation and traversal logic when upgrading to v0.24.0 or newer to ensure consistent behavior.","message":"Internal refactoring of edge & neighbor iteration schemes and index handling in v0.24.0 might lead to subtle behavioral changes or performance differences, especially in complex use cases involving self-loops or multigraphs, although no direct API breaks were documented.","severity":"gotcha","affected_versions":">=0.24.0"}],"env_vars":null,"last_verified":"2026-04-21T00:00:00.000Z","next_check":"2026-07-20T00:00:00.000Z","problems":[{"fix":"Change the import statement to `import Graph from 'graphology';` for ESM compatibility. If using TypeScript, ensure your `tsconfig.json`'s `module` option is set correctly (e.g., `\"ESNext\"` or `\"NodeNext\"`).","cause":"Attempting to use `require('graphology')` to import the `Graph` class in an environment that enforces ESM-style imports, or when the package's `package.json` entry points prioritize ESM.","error":"TypeError: Graph is not a constructor"},{"fix":"Install the peer dependency `graphology-types`: `npm install --save-dev graphology-types` or `yarn add -D graphology-types`. Ensure `tsconfig.json` includes `node_modules/@types` in `typeRoots` or that type declaration files are correctly resolved.","cause":"TypeScript compiler cannot locate the type definitions for `graphology`.","error":"TS2307: Cannot find module 'graphology' or its corresponding type declarations."},{"fix":"When creating the graph, explicitly set the `multi` option to `true`: `new Graph({ multi: true });` if you intend to allow multiple edges between the same nodes.","cause":"Attempting to add multiple edges between the same two nodes in a non-multi graph, or other multi-graph specific operations on a simple graph.","error":"Error: The given graph is not multi."}],"ecosystem":"npm"}