{"id":16963,"library":"cli-highlight","title":"Terminal Syntax Highlighter","description":"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.","status":"maintenance","version":"2.1.11","language":"javascript","source_language":"en","source_url":"https://github.com/felixfbecker/cli-highlight","tags":["javascript","terminal","syntax","highlight","color","cli","ansi","typescript"],"install":[{"cmd":"npm install cli-highlight","lang":"bash","label":"npm"},{"cmd":"yarn add cli-highlight","lang":"bash","label":"yarn"},{"cmd":"pnpm add cli-highlight","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Core dependency for syntax detection and tokenization, responsible for the actual highlighting logic.","package":"highlight.js","optional":false},{"reason":"Used for applying ANSI escape codes to terminal output, enabling colored highlighting based on themes.","package":"chalk","optional":false},{"reason":"Powers the command-line interface, handling argument parsing and help output.","package":"yargs","optional":false}],"imports":[{"note":"The primary highlighting function is a named export.","wrong":"import highlight from 'cli-highlight';","symbol":"highlight","correct":"import { highlight } from 'cli-highlight';"},{"note":"When using CommonJS, access the named export directly from the returned object.","wrong":"const highlight = require('cli-highlight');","symbol":"highlight (CommonJS)","correct":"const { highlight } = require('cli-highlight');"},{"note":"The package also installs a `highlight` executable for command-line usage.","symbol":"highlight (CLI executable)","correct":"highlight package.json"}],"quickstart":{"code":"import { highlight } from 'cli-highlight';\nimport { createServer } from 'http';\n\nconst server = createServer((req, res) => {\n  if (req.url === '/code' && req.method === 'GET') {\n    const codeExample = `function greet(name: string) {\n  console.log('Hello, ' + name + '!');\n}\n\ngreet('World');\n`;\n    console.log(highlight(codeExample, { language: 'typescript', ignoreIllegals: true }));\n    res.writeHead(200, { 'Content-Type': 'text/plain' });\n    res.end('Code highlighted in console.');\n  } else {\n    res.writeHead(200, { 'Content-Type': 'text/plain' });\n    res.end('Visit /code to see an example of highlighted code.');\n  }\n});\n\nserver.listen(3000, () => {\n  console.log('Server running on http://localhost:3000');\n  console.log('Make a GET request to http://localhost:3000/code to see highlighted output in your terminal.');\n});","lang":"typescript","description":"This example sets up a basic HTTP server that logs a TypeScript code snippet with syntax highlighting to the console when '/code' is accessed."},"warnings":[{"fix":"Review `highlight.js` v10 migration guide. `cli-highlight` v2.1.6+ handles internal updates, but external integrations might need adjustment.","message":"`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.","severity":"breaking","affected_versions":">=2.1.6"},{"fix":"Ensure your direct `chalk` usage is compatible with `chalk` v4's API, especially regarding ESM imports if you're targeting newer Node.js versions or specific `chalk` features.","message":"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.","severity":"breaking","affected_versions":">=2.1.8"},{"fix":"Use the `language` option: `highlight(code, { language: 'json' })` or `highlight --language js myFile.js`.","message":"Automatic language detection can sometimes be inaccurate for very short snippets or mixed content. Explicitly specifying the language often yields better results.","severity":"gotcha","affected_versions":">=2.0.0"},{"fix":"Upgrade to a currently maintained Node.js LTS version (e.g., Node.js 18+ or 20+).","message":"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.","severity":"deprecated","affected_versions":"<=2.1.11"}],"env_vars":null,"last_verified":"2026-04-22T00:00:00.000Z","next_check":"2026-07-21T00:00:00.000Z","problems":[{"fix":"Correct the import: `const { highlight } = require('cli-highlight');`","cause":"Attempting to call `require('cli-highlight')()` directly as a function instead of accessing the named `highlight` export.","error":"TypeError: highlight is not a function"},{"fix":"Either 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');`","cause":"Using `import { highlight } from 'cli-highlight';` in a CommonJS (`.js`) file without `\"type\": \"module\"` in `package.json`.","error":"SyntaxError: Cannot use import statement outside a module"},{"fix":"Ensure 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.","cause":"The terminal might not support true color (24-bit) or ANSI escape codes, or `cli-highlight`'s color detection is overridden.","error":"No highlighting visible in terminal"}],"ecosystem":"npm","meta_description":null}