Tree-sitter CLI

raw JSON →
0.26.8 verified Sat Apr 25 auth: no javascript

CLI tool for generating and testing Tree-sitter parsers, version 0.26.8. Released as part of the tree-sitter project, which provides a fast incremental parser framework. The CLI supports generating parser C code from grammar files, testing parsers, and building WASM modules. Key differentiators include high performance, incremental parsing with error recovery, and support for multiple languages via grammar definitions. Release cadence is frequent with minor bug fixes and improvements.

error Error: Cannot find module 'tree-sitter'
cause Native module not installed or incompatible
fix
npm install tree-sitter
error TypeError: Parser is not a constructor
cause Using default import as named import
fix
Use: const Parser = require('tree-sitter');
error Error: Language not loaded
cause WASM or native language file missing
fix
Ensure language module is correctly loaded (e.g., require('tree-sitter-javascript'))
breaking tree-sitter 0.20+ changed the grammar format; old grammars may not work
fix Update grammar files to new format (see migration guide)
gotcha require('tree-sitter') returns the native addon, not the web binding
fix Use 'web-tree-sitter' for browser or Node ESM
deprecated tree-sitter 0.25 deprecated the --no-ranges flag
fix Use --ranges or remove flag
npm install tree-sitter-cli
yarn add tree-sitter-cli
pnpm add tree-sitter-cli

Parse JavaScript source code using tree-sitter and print the syntax tree.

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

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

const sourceCode = 'let x = 1;';
const tree = parser.parse(sourceCode);
console.log(tree.rootNode.toString());
// (program (lexical_declaration (variable_declarator (identifier) (number))))