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.
npm install graphologyVerified import paths — ran on the pinned version, not inferred.
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.
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.
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.
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.
Thoroughly test existing graph manipulation and traversal logic when upgrading to v0.24.0 or newer to ensure consistent behavior.
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"`).
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.
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.