mrkeyoor.com_
Sat 08 Aug 20:58 UTC
npmWeb Frontendupdated 08 Aug 2026

sql-highlight

sql-highlight is a dependency-free SQL tokenizer and syntax colorizer for logs and web pages. Its `highlight` function wraps recognized tokens in ANSI color codes for terminals or escaped `<span>` elements for HTML, while `getSegments` exposes the token stream for custom rendering. It uses regular expressions and a keyword list rather than a dialect-aware SQL parser, so it is appropriate for display, not validation, formatting, query rewriting, or security analysis.

Verdict

A very small, practical choice for coloring SQL in Node logs or simple HTML. Do not mistake the flat regex token stream for a formatter, parser, or dialect-aware analysis tool.

API stability4/5Version 6.1.0 exposes only highlight, getSegments, and DEFAULT_OPTIONS, with straightforward option and segment types in the shipped declaration file. The repository notes that version 3 was an almost complete rewrite, so major releases have carried real change, but the present string-in, string-or-segments-out model is small enough to isolate and test. Custom renderers should still avoid assuming undocumented tokenization details.
Docs4/5The README gives terminal and HTML examples, lists every option and default ANSI color, shows the exact HTML class output, links sample CSS, and documents getSegments with a complete result. It also candidly notes the project's fork history. Missing pieces include ESM guidance, a dialect/support matrix, detailed edge cases, and an explicit warning that the shallow options merge makes partial colors objects surprising.
Maintenance5/5GitHub reports a push on August 3, 2026, only days before this guide's timestamp, and the repository is not archived. Version 6.1.0 was released in June 2025, CI and coverage are linked from the README, and the package has zero production dependencies to age underneath it. The current open_issues_count is two, which represents issues and pull requests together rather than a true issue-only count.
Ecosystem3/5The package records 3,998,384 weekly downloads, includes TypeScript declarations, and its stable CSS class names make it easy to add to small web or logging tools. Its scope is intentionally narrow, with no language registry, theme packages, framework adapters, or SQL dialect plugins. Teams already standardized on Prism, highlight.js, or Shiki gain little from maintaining a separate SQL-only path.

Use it if

  • You want readable SQL in Node.js logs with no runtime dependencies
  • You need small HTML output with stable token classes that you can style yourself
  • You want raw keyword, string, number, identifier, comment, and punctuation segments for a custom renderer
  • You need one synchronous API for both terminal colors and browser-oriented HTML
Skip it if

Setup reality

Install with `npm install sql-highlight`. There are no runtime dependencies, peers, native modules, credentials, or configuration files, and Bundlephobia measures the minified package at 1.5 KB gzipped. Version 6.1.0 declares Node 14 or newer and publishes a CommonJS entry with bundled TypeScript declarations, so the documented form is `const { highlight } = require('sql-highlight')`; bundlers can consume it, but there is no exports map or native ESM module entry. Terminal output is the default and contains ANSI escape codes, which means writing it to JSON logs, files, or non-color-aware collectors can leave control sequences in stored text. HTML output must be explicitly enabled with `{ html: true }`. It escapes token content with a built-in escaper before adding spans, but it supplies no stylesheet, so copy or adapt the sample CSS and keep the configured class prefix in sync. The tokenizer is lexical, not a SQL grammar: double-quoted text is treated as a string, backticks as identifiers, any word followed by optional whitespace and `(` as a function, and comments between a function name and its opening parenthesis are a documented unsupported case. Custom `colors` replaces the whole nested colors object because options are merged shallowly; spread `DEFAULT_OPTIONS.colors` when changing one color or the unspecified token types will lose coloring. The same caution applies to a custom `htmlEscaper`: returning unsafe text makes later `innerHTML` insertion unsafe.

Patterns

Color SQL for a terminalhighlight-terminal-sql

const { highlight } = require('sql-highlight')

const sql = 'SELECT id, email FROM users WHERE active = 1'
console.log(highlight(sql))

ANSI output is the default; avoid storing it in structured logs or files unless consumers expect control codes.

Render SQL as escaped HTML spanshighlight-html

const { highlight } = require('sql-highlight')

const html = highlight(sql, { html: true })
codeElement.innerHTML = html

Token content is escaped by default, but the package does not include CSS in its published files.

Style generated token classesstyle-html-output

.sql-hl-keyword { color: #c678dd; }
.sql-hl-function { color: #61afef; }
.sql-hl-string, .sql-hl-number { color: #98c379; }
.sql-hl-comment { color: #7f848e; font-style: italic; }
.sql-hl-special, .sql-hl-bracket { color: #abb2bf; }

HTML mode emits only spans; define classes for every token type your theme needs.

Use namespaced HTML classeschange-class-prefix

const html = highlight(sql, {
  html: true,
  classPrefix: 'query-token-',
})

The emitted classes become query-token-keyword, query-token-string, and so on, so update CSS at the same time.

Provide a DOM-based HTML escapercustom-html-escaper

const html = highlight(sql, {
  html: true,
  htmlEscaper(value) {
    const node = document.createElement('div')
    node.textContent = value
    return node.innerHTML
  },
})

A custom escaper runs for every token. It must escape, not merely return the input, when output is assigned through innerHTML.

Change one ANSI color safelycustomize-one-color

const { highlight, DEFAULT_OPTIONS } = require('sql-highlight')

const output = highlight(sql, {
  colors: {
    ...DEFAULT_OPTIONS.colors,
    keyword: '\x1b[36m',
  },
})

Options use a shallow merge. Passing only `{ keyword: ... }` without spreading defaults removes the other configured colors.

Produce unchanged-looking terminal textdisable-terminal-colors

const { highlight } = require('sql-highlight')

const output = highlight(sql, {
  colors: {
    keyword: '', function: '', number: '', string: '', identifier: '',
    special: '', bracket: '', comment: '', clear: '',
  },
})

For plain output, keeping the original SQL is cheaper; this pattern is useful only when one rendering pipeline requires highlight().

Get flat SQL segmentstokenize-sql

const { getSegments } = require('sql-highlight')

for (const segment of getSegments(sql)) {
  console.log(segment.name, JSON.stringify(segment.content))
}

Segments are lexical name/content pairs, not an AST and not proof that the SQL is valid.

Collect recognized keywordscollect-sql-keywords

const { getSegments } = require('sql-highlight')

const keywords = getSegments(sql)
  .filter(({ name }) => name === 'keyword')
  .map(({ content }) => content.toUpperCase())

Recognition uses the library's built-in keyword list and is case-insensitive; there is no per-dialect list option.

Render segments as React elementsrender-react-segments

import { getSegments } from 'sql-highlight'

function SqlCode({ sql }) {
  return <code>{getSegments(sql).map((part, index) => (
    <span className={`sql-hl-${part.name}`} key={index}>{part.content}</span>
  ))}</code>
}

React escapes part.content automatically; do not convert the result to an HTML string first.

Highlight a multiline queryhighlight-multiline-query

const output = highlight(`
  SELECT u.id, COUNT(o.id)
  FROM users u
  LEFT JOIN orders o ON o.user_id = u.id
  GROUP BY u.id
`)
console.log(output)

Whitespace is retained exactly. This highlighter does not indent or reformat the query.

Color SQL commentshighlight-comments

const output = highlight(`
-- active customers only
SELECT * FROM customers WHERE active = 1;
/* review this predicate */
`)
console.log(output)

The tokenizer recognizes double-dash, hash, and block comments, but it does not apply dialect-specific comment rules.

Alternatives

PackageRegistryPick it when
sql-formatternpmChoose it when consistent indentation and dialect-aware formatting matter more than terminal colors
highlight.jsnpmChoose it when the same automatic highlighter must cover SQL plus many other languages
shikinpmChoose it for editor-grade tokenization and rich themes in documentation or build-time rendering
prismjsnpmChoose it for client-side code blocks in an existing Prism theme and plugin setup