Terminal Syntax Highlighter
cli-highlight is a utility for applying syntax highlighting to code and text within a terminal environment, leveraging the capabilities of highlight.js for language detection and tokenization. It provides both a command-line interface for piping input or reading files and a programmatic API for integrating highlighting into Node.js applications. The package is currently at version 2.1.11, with its last update in March 2021, suggesting a maintenance rather than active feature development cadence. Key differentiators include its robust language support inherited from highlight.js, the ability to define custom themes using Chalk styles for fine-grained control over terminal colors, and its seamless integration with other CLI tools through standard input/output piping. It is particularly useful for debugging, logging, or displaying code snippets in a more readable format directly in the console.
Common errors
-
TypeError: highlight is not a function
cause Attempting to call `require('cli-highlight')()` directly as a function instead of accessing the named `highlight` export.fixCorrect the import: `const { highlight } = require('cli-highlight');` -
SyntaxError: Cannot use import statement outside a module
cause Using `import { highlight } from 'cli-highlight';` in a CommonJS (`.js`) file without `"type": "module"` in `package.json`.fixEither convert your project to ESM by adding `"type": "module"` to `package.json` and ensure all dependencies are ESM-compatible, or use the CommonJS `require` syntax: `const { highlight } = require('cli-highlight');` -
No highlighting visible in terminal
cause The terminal might not support true color (24-bit) or ANSI escape codes, or `cli-highlight`'s color detection is overridden.fixEnsure your terminal emulator supports sufficient color depth. You can force color output with `highlight --color` (if such an option existed, which it doesn't by default, indicating `chalk`'s level detection is key) or ensure `process.env.TERM` is set correctly for your terminal. Check the `chalk` library's documentation on forcing color levels.
Warnings
- breaking `cli-highlight` relies on `highlight.js` v10+ which itself introduced breaking changes. If you are directly interacting with `highlight.js` through `cli-highlight` (e.g., custom languages/plugins), ensure compatibility with `highlight.js` v10's API.
- breaking The update to `chalk` v4 in `cli-highlight` v2.1.8 might indirectly affect applications that also use `chalk` directly and relied on specific internal behaviors or API patterns removed in `chalk` v4.
- gotcha Automatic language detection can sometimes be inaccurate for very short snippets or mixed content. Explicitly specifying the language often yields better results.
- deprecated Older Node.js versions (e.g., pre-10 or 12) might still function but are not officially supported or tested with recent dependency updates. The `engines.node` specifies `>=8.0.0`, but this is very old.
Install
-
npm install cli-highlight -
yarn add cli-highlight -
pnpm add cli-highlight
Imports
- highlight
import highlight from 'cli-highlight';
import { highlight } from 'cli-highlight'; - highlight (CommonJS)
const highlight = require('cli-highlight');const { highlight } = require('cli-highlight'); - highlight (CLI executable)
highlight package.json
Quickstart
import { highlight } from 'cli-highlight';
import { createServer } from 'http';
const server = createServer((req, res) => {
if (req.url === '/code' && req.method === 'GET') {
const codeExample = `function greet(name: string) {
console.log('Hello, ' + name + '!');
}
greet('World');
`;
console.log(highlight(codeExample, { language: 'typescript', ignoreIllegals: true }));
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Code highlighted in console.');
} else {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Visit /code to see an example of highlighted code.');
}
});
server.listen(3000, () => {
console.log('Server running on http://localhost:3000');
console.log('Make a GET request to http://localhost:3000/code to see highlighted output in your terminal.');
});