# tree-sitter

> Node.js bindings to the Tree-sitter parsing library

Latest version **0.25.1** (published 2026-07-28) · MIT license · 2.8M weekly downloads

## Install

```sh
npm install tree-sitter
pnpm add tree-sitter
yarn add tree-sitter
bun add tree-sitter
```

## Health

**Score 85/100 (A)** — status: active.

Positive: high downloads; has types; no vulnerabilities; has provenance; recently updated; high quality score.

Warnings: no esm support; pre 1.0.

## Facts

| | |
|---|---|
| Version | 0.25.1 |
| Published | 2026-07-28 |
| First published | 2014-07-22 |
| Weekly downloads | 2.8M |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 4.2 MB |
| Known vulnerabilities | 0 |
| Install scripts | yes |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 878 |
| Author | Max Brunsfeld |
| Maintainers | ahlinc, maxbrunsfeld, amaanq, tclem, rewinfrey, robrix, joshvera, patrickthomson, verhovsky |
| Keywords | incremental, parsing, tree-sitter |

## Links

- npm: https://www.npmjs.com/package/tree-sitter
- Repository: https://github.com/tree-sitter/node-tree-sitter
- Homepage: https://github.com/tree-sitter/node-tree-sitter#readme
- Issues: https://github.com/tree-sitter/node-tree-sitter/issues
- npm.io page: https://npm.io/package/tree-sitter

## Dependencies (2)

- [node-addon-api](https://npm.io/package/node-addon-api.md) ^8.5.0
- [node-gyp-build](https://npm.io/package/node-gyp-build.md) ^4.8.4

## Recent versions

- 0.25.1 (latest) — 2026-07-28
- 0.25.0 — 2025-06-02
- 0.22.4 — 2024-12-30
- 0.22.3 — 2024-12-30
- 0.22.2 — 2024-12-30
- 0.22.1 — 2024-11-10
- 0.22.0 — 2024-10-26
- 0.21.1 — 2024-03-28
- 0.21.0 — 2024-03-10
- 0.20.6 — 2023-10-16
- 0.20.5 — 2023-07-03
- 0.20.4 — 2023-06-24
- 0.20.3 — 2023-06-22
- 0.20.2 — 2023-06-14
- 0.20.1 — 2022-10-26
- … 186 more at https://npm.io/package/tree-sitter/versions

## README

# Node Tree-sitter

[![CI][ci]](https://github.com/tree-sitter/node-tree-sitter/actions/workflows/ci.yml)
[![npm][npm]](https://npmjs.com/package/tree-sitter)
[![docs][docs]](https://tree-sitter.github.io/node-tree-sitter)

This module provides Node.js bindings to the [tree-sitter] parsing library.

## Installation

```sh
npm install tree-sitter
```

## Basic Usage

### Prerequisites

First, you'll need a Tree-sitter grammar for the language you want to parse. There are many [existing grammars][grammars],
such as [tree-sitter-javascript][javascript]. These grammars can typically be installed with a package manager like NPM,
so long as the author has published them.

```sh
npm install tree-sitter-javascript
```

You can also develop a new grammar by using the [Tree-sitter CLI][cli] and following the [docs][ts docs].

### Parsing Source Code

Once you've got your grammar, create a parser with that grammar.

```javascript
const Parser = require('tree-sitter');
const JavaScript = require('tree-sitter-javascript');

const parser = new Parser();
parser.setLanguage(JavaScript);
```

Then you can parse some source code,

```javascript
const sourceCode = 'let x = 1; console.log(x);';
const tree = parser.parse(sourceCode);
```

and inspect the syntax tree.

```javascript
console.log(tree.rootNode.toString());

// (program
//   (lexical_declaration
//     (variable_declarator (identifier) (number)))
//   (expression_statement
//     (call_expression
//       (member_expression (identifier) (property_identifier))
//       (arguments (identifier)))))

const callExpression = tree.rootNode.child(1).firstChild;
console.log(callExpression);

// {
//   type: 'call_expression',
//   startPosition: {row: 0, column: 16},
//   endPosition: {row: 0, column: 30},
//   startIndex: 0,
//   endIndex: 30
// }
```

If your source code *changes*, you can update the syntax tree. This is much faster than the first parse.

```javascript
// In the code, we replaced 'let' with 'const'.
// So, we set our old end index to 3, and our new end index to 5.
// Note that the end index is exclusive.
const newSourceCode = 'const x = 1; console.log(x);';
//                        ^ ^
// indices:               3 5
// points:            (0,3) (0,5)

tree.edit({
  startIndex: 0,
  oldEndIndex: 3,
  newEndIndex: 5,
  startPosition: {row: 0, column: 0},
  oldEndPosition: {row: 0, column: 3},
  newEndPosition: {row: 0, column: 5},
});

const newTree = parser.parse(newSourceCode, tree);
```

### Parsing Text From a Custom Data Structure

If your text is stored in a data structure other than a single string, such as a rope or array, you can parse it by supplying
a callback to `parse` instead of a string:

```javascript
const sourceLines = [
  'let x = 1;',
  'console.log(x);'
];

const tree = parser.parse((index, position) => {
  let line = sourceLines[position.row];
  if (line) {
    return line.slice(position.column);
  }
});
```

### Further Reading

It's recommended that you read the [Tree-sitter documentation][usage docs] on using parsers to get a higher-level overview
of the API. Once you're comfortable with the basics, you can explore the [full API documentation](https://tree-sitter.github.io/node-tree-sitter),
which should map closely to the C API, though there are some differences.

[ci]: https://img.shields.io/github/actions/workflow/status/tree-sitter/node-tree-sitter/ci.yml?logo=github&label=CI
[cli]: https://github.com/tree-sitter/tree-sitter/tree/master/cli
[docs]: https://img.shields.io/badge/docs-website-blue
[npm]: https://img.shields.io/npm/v/tree-sitter?logo=npm
[grammars]: https://github.com/tree-sitter/tree-sitter/wiki/List-of-parsers
[javascript]: http://github.com/tree-sitter/tree-sitter-javascript
[ts docs]: https://tree-sitter.github.io/tree-sitter/creating-parsers
[usage docs]: https://tree-sitter.github.io/tree-sitter/using-parsers

---
_Source: https://npm.io/package/tree-sitter · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
