Skip to content

Node.js

Use @comfit/node to highlight code on a server or in a build script. It provides synchronous functions with bundled languages and themes, and supports ESM on Node.js 22 or later.

Install

Shell
Shell
pnpm add @comfit/node

The package uses a platform-specific native addon. If you are working from a checkout, build the addon first.

Render HTML

Save this as highlight.mjs:

highlight.mjsJavaScript
import { renderHtml, RenderHtmlConfig, RenderHtmlStyle } from "@comfit/node";

const config = new RenderHtmlConfig();
config.style = RenderHtmlStyle.Inline;

const html = renderHtml(
  "const answer = 42;",
  "javascript",
  "catppuccin-mocha",
  config,
);

console.log(html);

Run node highlight.mjs to print an HTML fragment with inline colors. Your page's CSS controls its font, padding, and layout.

For HTML rendering, pass a RenderHtmlConfig instance, not a plain object. The default style is classes, which requires a theme stylesheet. Selecting Inline embeds the colors in the fragment.

Find languages and themes

JavaScript
import { containsLanguage, languages, themes } from "@comfit/node";

console.log(languages());
console.log(themes());
console.log(containsLanguage("rs"));

Language IDs and aliases are accepted, such as rust or rs. An unknown root language or theme throws an error.

Choose another output

highlight(source, language) returns lines and tokens. renderAnsi(source, language, theme) returns styled terminal text. For class-based HTML, use renderHtmlClasses with a CSS theme. For Markdown or MDX code blocks, use rehype.

Calls reuse a cached highlighter and block the calling JavaScript thread. Use a Node worker for large inputs. For repeated edits to one source, use a document session.

See the Node.js API reference for configuration fields and methods.