{"id":10749,"library":"dijkstrajs","title":"Dijkstra's Algorithm Implementation","description":"dijkstrajs is a lightweight, zero-dependency JavaScript library that provides a straightforward implementation of Dijkstra's single-source shortest-paths algorithm. Currently at version 1.0.3, the package was last published over three years ago, indicating a mature and stable codebase rather than active feature development. It is designed for use in Node.js environments and offers a simple API for finding the shortest path between two nodes in a weighted graph. While other Dijkstra implementations might offer performance optimizations like priority queues, dijkstrajs prioritizes simplicity and ease of integration for basic graph traversal needs. Developers should note its inherent limitation of not supporting negative edge weights, which is a fundamental constraint of Dijkstra's algorithm itself.","status":"maintenance","version":"1.0.3","language":"javascript","source_language":"en","source_url":"git://github.com/tcort/dijkstrajs","tags":["javascript","dijkstra","shortest","path","search","graph"],"install":[{"cmd":"npm install dijkstrajs","lang":"bash","label":"npm"},{"cmd":"yarn add dijkstrajs","lang":"bash","label":"yarn"},{"cmd":"pnpm add dijkstrajs","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The library exports an object with a 'findPath' method, not a default function. This applies to both ESM and CJS.","wrong":"import dijkstra from 'dijkstrajs'","symbol":"findPath","correct":"import { findPath } from 'dijkstrajs'"},{"note":"For CommonJS, destructure the 'findPath' method from the required module object.","wrong":"const dijkstra = require('dijkstrajs')","symbol":"findPath (CommonJS)","correct":"const { findPath } = require('dijkstrajs')"}],"quickstart":{"code":"import { findPath } from 'dijkstrajs';\n\nconst graph = {\n  A: { B: 1, C: 4 },\n  B: { A: 1, C: 2, D: 5 },\n  C: { A: 4, B: 2, D: 1 },\n  D: { B: 5, C: 1 },\n  E: { F: 3 }, // Disconnected node\n  F: { E: 3 }\n};\n\n// Find the shortest path from node A to node D\nconst pathAD = findPath(graph, 'A', 'D');\nconsole.log('Path from A to D:', pathAD); // Expected: [ 'A', 'B', 'C', 'D' ]\n\n// Find the shortest path from node A to node E (unreachable)\nconst pathAE = findPath(graph, 'A', 'E');\nconsole.log('Path from A to E:', pathAE); // Expected: null (or similar indication of no path)\n\n// Example with different start/end\nconst pathBD = findPath(graph, 'B', 'D');\nconsole.log('Path from B to D:', pathBD); // Expected: [ 'B', 'C', 'D' ]\n","lang":"javascript","description":"This example demonstrates how to define a weighted graph using an adjacency list and then use `findPath` to calculate the shortest path between specified start and end nodes."},"warnings":[{"fix":"Ensure all edge weights in your graph are non-negative, or use an alternative algorithm for graphs with negative weights.","message":"Dijkstra's algorithm, and consequently this library, does not correctly compute shortest paths if the graph contains edges with negative weights. For graphs with negative weights, consider algorithms like Bellman-Ford or SPFA.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"For performance-critical applications with large graphs, benchmark this library against alternatives that explicitly mention priority queue optimizations.","message":"The library is a 'simple implementation' and does not explicitly state the use of an optimized data structure like a min-priority queue. For very large or dense graphs, performance might be slower than implementations that utilize such optimizations (e.g., O(E log V) vs. O(V^2)).","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Refer to the quickstart example for the precise graph object structure required by `dijkstrajs`.","message":"The expected graph input format is an adjacency list represented as a JavaScript object, where keys are node names and values are objects mapping neighbor nodes to their respective edge weights. Deviating from this format will lead to incorrect results or errors.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Consider testing thoroughly with newer Node.js runtimes. For projects requiring active maintenance or advanced features, explore more recently updated graph libraries.","message":"The package has not been updated in over three years (last published 3+ years ago). While stable, this indicates a lack of active development, bug fixes, or new features. Users should be aware that new Node.js versions or JavaScript features might not be officially supported or tested.","severity":"deprecated","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Use a named import for ESM: `import { findPath } from 'dijkstrajs'`. For CommonJS, ensure proper destructuring: `const { findPath } = require('dijkstrajs')`.","cause":"Attempting to import `findPath` using a default import or a CommonJS `require` call that doesn't destructure the `findPath` property from the module object in an ESM context.","error":"TypeError: (0 , dijkstrajs__WEBPACK_IMPORTED_MODULE_0__.findPath) is not a function"},{"fix":"Verify the `require` statement accurately destructures `findPath`: `const { findPath } = require('dijkstrajs')`. Ensure the package is correctly installed.","cause":"This typically occurs in CommonJS environments when `require('dijkstrajs')` is assigned to a variable, and then `.findPath` is called on it, but the default export isn't the expected object, or the import path is incorrect.","error":"TypeError: Cannot read properties of undefined (reading 'findPath')"},{"fix":"Validate that the source and target nodes exist in the graph and that there is a connected path between them. Handle the `null` return explicitly in your application logic.","cause":"The `findPath` function returns `null` or an empty path when the target node is unreachable from the source node within the given graph, or if the graph is disconnected.","error":"Path not found (or similar message/null return for unreachable nodes)"}],"ecosystem":"npm"}