{"id":1749,"library":"tree-sitter-javascript","title":"Tree-sitter JavaScript Grammar","description":"The `tree-sitter-javascript` package provides the JavaScript grammar for the `tree-sitter` parsing library. It allows Python developers to easily load and use the official Tree-sitter JavaScript grammar to parse JavaScript code into concrete syntax trees (CSTs). The current version is 0.25.0, and it follows the release cadence of the upstream Tree-sitter JavaScript grammar, with updates for syntax changes and bug fixes.","status":"active","version":"0.25.0","language":"en","source_language":"en","source_url":"https://github.com/tree-sitter/tree-sitter-javascript","tags":["syntax-tree","parser","javascript","tree-sitter","grammar","ast"],"install":[{"cmd":"pip install tree-sitter-javascript tree-sitter","lang":"bash","label":"Install with core library"}],"dependencies":[{"reason":"The core Python `tree-sitter` library is required to create parsers and interact with the grammar.","package":"tree-sitter","optional":false}],"imports":[{"symbol":"language","correct":"from tree_sitter_javascript import language"}],"quickstart":{"code":"import tree_sitter\nfrom tree_sitter_javascript import language\n\n# 1. Get the JavaScript language object\nJS_LANGUAGE = language()\n\n# 2. Create a parser and set the language\nparser = tree_sitter.Parser()\nparser.set_language(JS_LANGUAGE)\n\n# 3. Parse some JavaScript code (must be bytes)\ncode = b\"\"\"\nfunction greet(name) {\n  console.log(\"Hello, \" + name + \"!\");\n}\ngreet(\"World\");\n\"\"\"\ntree = parser.parse(code)\n\n# 4. Access the root node and explore the tree\nroot_node = tree.root_node\n\nprint(f\"Root node type: {root_node.type}\")\nprint(f\"Root node content (first 50 chars): {root_node.text.decode('utf8')[:50]}...\")\n\n# Example: Find the 'function_declaration' node\nfunction_node = None\nfor child in root_node.children:\n    if child.type == 'function_declaration':\n        function_node = child\n        break\n\nif function_node:\n    function_name_node = function_node.child_by_field_name('name')\n    if function_name_node:\n        print(f\"Found function name: {function_name_node.text.decode('utf8')}\")\n","lang":"python","description":"This quickstart demonstrates how to load the JavaScript grammar, create a parser, and parse a simple JavaScript code snippet. It then shows how to access the root node and find a specific node type, such as a function declaration, illustrating basic tree traversal."},"warnings":[{"fix":"Ensure you install both `tree-sitter-javascript` and `tree-sitter`: `pip install tree-sitter-javascript tree-sitter`.","message":"The `tree-sitter-javascript` package *provides* the grammar, but it inherently depends on the core `tree-sitter` Python library to function. Many users forget to install `tree-sitter` alongside this grammar package, leading to `ModuleNotFoundError` for `tree_sitter` or issues when trying to instantiate `Parser` objects. Always install both.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Ensure a C compiler is installed and available in your environment before attempting installation. For Debian/Ubuntu: `apt-get install build-essential`. For macOS: `xcode-select --install`.","message":"The underlying `tree-sitter` core library (which `tree-sitter-javascript` leverages) is a C extension. While pre-built wheels are available for many common platforms, certain environments (e.g., specific Linux distributions, custom Python builds, or older Python versions) may require a C compiler (like GCC or Clang) during installation. Lack of a compiler can cause installation failures.","severity":"gotcha","affected_versions":"All versions"},{"fix":"It is strongly recommended to pin both the `tree-sitter-javascript` and `tree-sitter` package versions in your `requirements.txt` or project dependencies to ensure consistent behavior across deployments (e.g., `tree-sitter-javascript==0.25.0`, `tree-sitter==0.21.1`).","message":"While the `language()` function provided by `tree-sitter-javascript` is generally stable, major version changes in the upstream `tree-sitter` Python library can introduce breaking API changes for `Parser`, `Tree`, or `Node` objects. Similarly, significant updates to the underlying JavaScript grammar definition (less frequent for the Python binding) could subtly change the structure of the generated ASTs.","severity":"breaking","affected_versions":"All versions, especially across major `tree-sitter` updates."}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}