{"id":11128,"library":"javascript-astar","title":"A* Pathfinding Algorithm","description":"The `javascript-astar` package provides an efficient implementation of the A* search algorithm in JavaScript for finding the shortest path on a grid. Currently at version 0.4.1, it has been optimized to use a Binary Heap, resulting in significant performance improvements over its original list-based approach. The library supports various graph configurations, including weighted nodes and diagonal movement. A weight of 0 for a node designates it as an impassable obstacle, and specific constraints apply to other weight values. It is primarily designed for browser environments via script tags, exposing global variables, and likely supports CommonJS environments for server-side usage, given its version and use of Grunt for tooling.","status":"maintenance","version":"0.4.1","language":"javascript","source_language":"en","source_url":"git://github.com/bgrins/javascript-astar","tags":["javascript"],"install":[{"cmd":"npm install javascript-astar","lang":"bash","label":"npm"},{"cmd":"yarn add javascript-astar","lang":"bash","label":"yarn"},{"cmd":"pnpm add javascript-astar","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The `astar` object contains the main `search` function and `heuristics` object. In browser environments, `astar` is available as a global variable.","wrong":"import astar from 'javascript-astar'; // Incorrect for named export\nconst astar = require('javascript-astar'); // This assigns the full module object, not just the 'astar' property","symbol":"astar","correct":"import { astar } from 'javascript-astar'; // ESM\nconst { astar } = require('javascript-astar'); // CJS"},{"note":"The `Graph` class is used to construct the grid for pathfinding. In browser environments, `Graph` is available as a global variable.","wrong":"new astar.Graph(); // Incorrect, `Graph` is a separate top-level export/global, not a property of `astar`","symbol":"Graph","correct":"import { Graph } from 'javascript-astar'; // ESM\nconst { Graph } = require('javascript-astar'); // CJS"},{"note":"Access specific heuristics as properties of the `astar.heuristics` object.","wrong":"import { diagonal } from 'javascript-astar'; // Heuristics are nested under the `astar` object","symbol":"astar.heuristics.diagonal","correct":"import { astar } from 'javascript-astar';\nconst diagonalHeuristic = astar.heuristics.diagonal;"}],"quickstart":{"code":"const { astar, Graph } = require('javascript-astar'); // For Node.js or bundlers; for browsers, `astar` and `Graph` are globals.\n\n// Basic usage: Find path on a grid without diagonal movement or custom weights.\nvar graph = new Graph([\n\t[1,1,1,1],\n\t[0,1,1,0],\n\t[0,0,1,1]\n]);\nvar start = graph.grid[0][0];\nvar end = graph.grid[1][2];\nvar result = astar.search(graph, start, end);\nconsole.log('Path (no diagonals, no weight):', result.map(node => `(${node.x},${node.y})`).join(' -> '));\n\n// With diagonals: Enable diagonal moves and use the diagonal heuristic.\nvar graphDiagonal = new Graph([\n\t[1,1,1,1],\n\t[0,1,1,0],\n\t[0,0,1,1]\n], { diagonal: true });\nvar startDiagonal = graphDiagonal.grid[0][0];\nvar endDiagonal = graphDiagonal.grid[1][2];\nvar resultWithDiagonals = astar.search(graphDiagonal, startDiagonal, endDiagonal, { heuristic: astar.heuristics.diagonal });\nconsole.log('Path (with diagonals):', resultWithDiagonals.map(node => `(${node.x},${node.y})`).join(' -> '));\n\n// With weights: Define custom movement costs for nodes (0 for walls).\nvar graphWithWeight = new Graph([\n\t[1,1,2,30],\n\t[0,4,1.3,0],\n\t[0,0,5,1]\n]);\nvar startWithWeight = graphWithWeight.grid[0][0];\nvar endWithWeight = graphWithWeight.grid[1][2];\nvar resultWithWeight = astar.search(graphWithWeight, startWithWeight, endWithWeight);\nconsole.log('Path (with weights):', resultWithWeight.map(node => `(${node.x},${node.y})`).join(' -> '));","lang":"javascript","description":"Demonstrates initializing a graph, performing basic A* search, and utilizing options for diagonal movement and custom node weights."},"warnings":[{"fix":"Upgrade to a newer version (0.4.1 or later) to benefit from the performance improvements. Review existing code for any direct reliance on the internal algorithm structures of versions prior to 0.0.1.","message":"The internal algorithm was fundamentally changed from version 0.0.1 to use a Binary Heap, significantly improving performance over the original list-based approach. While this is a positive change, it represents a breaking change in internal implementation details.","severity":"breaking","affected_versions":"<=0.0.1"},{"fix":"Ensure all graph node weights adhere to the specified rules: `0` for walls, `1` for standard passable nodes, or any number `> 1` (including decimals) for custom movement costs. Avoid negative weights or weights between 0 and 1 (exclusive).","message":"Node weights in the graph have specific constraints: a weight of 0 marks a node as an impassable 'wall'. Weights cannot be negative, nor can they be fractional values exclusively between 0 and 1 (e.g., 0.5 is invalid). Decimal values greater than 1 are permitted.","severity":"gotcha","affected_versions":">=0.0.1"},{"fix":"To avoid global conflicts, consider using a module bundler (like Webpack or Rollup) to integrate `javascript-astar` as a module. Alternatively, encapsulate its usage within an Immediately Invoked Function Expression (IIFE) in browser scripts to limit variable scope.","message":"When included directly in browser environments via a `<script>` tag, the library exposes `astar` and `Graph` as global variables. This can lead to global namespace pollution, potentially conflicting with other scripts or libraries.","severity":"gotcha","affected_versions":">=0.0.1"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"In browsers, ensure `<script src='astar.js'></script>` is present and loaded before your code. In Node.js, use `const { astar } = require('javascript-astar');` (for CJS) or `import { astar } from 'javascript-astar';` (for ESM).","cause":"The `astar` object was not correctly loaded or imported into the current scope. This typically happens if the script tag is missing in the browser or the CommonJS/ESM import is incorrect in Node.js/bundled environments.","error":"ReferenceError: astar is not defined"},{"fix":"In module environments, ensure `Graph` is destructured from the package: `const { Graph } = require('javascript-astar');` or `import { Graph } from 'javascript-astar';`. In browsers, verify the `astar.js` script is loaded to expose the global `Graph`.","cause":"The `Graph` class was not properly imported or made available. In module environments, this often means it wasn't destructured correctly during the `require` or `import` statement.","error":"TypeError: Graph is not a constructor"},{"fix":"Review the weights defined in your `Graph` grid. Valid weights are `0` (for walls), `1`, or any number `> 1` (including decimals). Adjust any invalid weight values accordingly.","cause":"A node in the `Graph` was initialized or modified with a weight value that violates the library's constraints (negative, or a decimal between 0 and 1 exclusively).","error":"Invalid weight value (e.g., 'Weight cannot be negative', 'Weight cannot be between 0 and 1 exclusive')"}],"ecosystem":"npm"}