The Tree-sitter JavaScript/Wasm bindings provide a way to use Tree-sitter in JavaScript environments, including browsers and Node.js. The library is compiled to WebAssembly for performance and portability.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/tree-sitter/tree-sitter/llms.txt
Use this file to discover all available pages before exploring further.
Installation
Install theweb-tree-sitter package from npm:
Basic Usage
Initialization
Before creating a parser, you must initialize the library:Creating a Parser
Loading a Language
Parsing Code
Core Types
Point
A position in a multi-line text document, in terms of rows and columns.Range
A range of positions in a multi-line text document.ParseCallback
A callback function for parsing text from custom data structures:Main Classes
The JavaScript/Wasm API consists of several main classes:- Parser - The main parsing interface
- Tree - Represents a syntax tree
- Node - Represents a single node in the tree
- Query - Execute pattern queries on syntax trees
- Language - Represents a grammar (loaded from
.wasmfiles) - Edit - Represents an edit operation for incremental parsing
Version Compatibility
The library exports version constants for checking language compatibility:Memory Management
The JavaScript bindings use WebAssembly, which requires explicit memory management for some objects:FinalizationRegistry when available to automatically clean up resources, but explicitly calling delete() is recommended for better control over memory usage.
Browser vs Node.js
The library works in both browsers and Node.js. However, note that:- In Node.js, native bindings (
node-tree-sitter) are generally faster - In browsers, you may need to configure your bundler to handle the
.wasmfiles - WASM files must be served with the correct MIME type (
application/wasm)
Next Steps
- Learn about the Parser class
- Explore Tree operations
- Understand Node traversal
- Execute pattern queries