Skip to main content
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.

Installation

Install the web-tree-sitter package from npm:
For Deno:

Basic Usage

Initialization

Before creating a parser, you must initialize the library:
With module options (e.g., for Next.js):

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:
Example:

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 .wasm files)
  • 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:
The library uses 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 .wasm files
  • WASM files must be served with the correct MIME type (application/wasm)

Next Steps