npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

nodejs-tree-sitter-highlight

v0.0.0

Published

Syntax highlighting with Tree-sitter (Node.js binding)

Readme

Node.js binding

Provides syntax highlighting with Tree-sitter from Node.js, mirroring the Python binding.

import { createRequire } from "node:module";
const require = createRequire(import.meta.url);

const tsPython = require("tree-sitter-python");
const { search_parsers, highlight } = require("tree-sitter-highlight");

// Discover the parsers. `search_parsers` reads each grammar package's `queries/*.scm`
// files from disk and keeps the package's `language` object for later pointer extraction.
const parsers = search_parsers({
  python: tsPython,
  // pass `javascript: require("tree-sitter-javascript")` to add more languages
});

const code = highlight({
  source: "x = 1\ndef foo():\n    return x\n",
  language: "python",
  parsers,
  theme: { variable: { color: "#F8F8F2" }, function: { color: "#FF0000" } },
  format: "html",
  layout: "fragment",
  style: "classes",
});

console.log(code);

API

search_parsers(parsers)

parsers is an object mapping a language name to an imported tree_sitter_* grammar package (the value of require("tree-sitter-python")).

Returns { lang: { language, highlights, injections, locals } }, where:

  • language is the grammar package's language object (the napi_external holding its TSLanguage*), kept so highlight can recover the pointer later;
  • highlights / injections / locals are the full text of the corresponding queries/{name}.scm files (empty string when a file is absent).

Grammar packages that expose no usable language are skipped silently, so they do not appear in the result.

highlight(options)

options:

| field | type | default | notes | | ------------- | ------------------------------- | ------------ | --------------------------------------- | | source | string | — | literal text; if omitted, file is read | | file | string | — | path to read ("-" means stdin) | | language | string | — | language key into parsers | | parsers | object | — | the search_parsers output | | theme | object | config theme | name -> { color, bold, ... } | | format | "html"\|"latex"\|"terminal" | "html" | | | layout | "document"\|"line-numbers"\|"fragment" | "document" | ignored for terminal | | style | "classes"\|"inline"\|"minimal" | "classes" | ignored for terminal | | prefix | string | "" | CSS/class prefix | | math_escape | string[] | [] | scope names whose LaTeX escapes enabled |

Returns the highlighted document as a string.

Building

The native addon is a Rust crate compiled with napi-rs. Prebuilt binaries are produced with prebuildify, which compiles via napi and lays the artifact out under prebuilds/<platform>-<arch>/ so it can be loaded at runtime by node-gyp-build (the loader that pairs with prebuildify).

npm install
npm run build          # napi build -> tree_sitter_highlight.<triple>.node (local dev)
npm run prebuildify    # napi build + copy into prebuilds/linux-x64/tree_sitter_highlight.node
npm test

index.js loads the addon through node-gyp-build(__dirname), preferring the prebuilds/ layout and falling back to a cargo-built cdylib (target/{debug,release}/libtree_sitter_highlight.so) when no prebuild is present.