CLI-High: Terminal Syntax Highlighter
cli-high is a lightweight, MIT-licensed JavaScript library providing syntax highlighting specifically designed for terminal output. It is currently at version 0.4.3 and appears to be in active development, having been introduced around 2024. As a "tiny" utility, it prioritizes minimal dependencies and a focused scope, primarily offering a single `highlight` function for programmatic use. The library ships with TypeScript types, enabling a robust development experience in TypeScript projects. While its release cadence isn't explicitly defined, as a 0.x.x version, users should anticipate potential API changes in minor or even patch releases. Its primary differentiators are its small footprint and dedicated purpose for aesthetic CLI output, rather than complex, browser-based highlighting solutions.
Common errors
-
TypeError: (0, cli_high__WEBPACK_IMPORTED_MODULE_0__.highlight) is not a function
cause This error typically occurs in bundled environments (like Webpack) when a named export is incorrectly treated as a default export, or when the `highlight` function is not correctly tree-shaken/imported.fixVerify that you are using a named import: `import { highlight } from 'cli-high'`. Check your bundler's configuration for ESM compatibility and ensure no conflicting default imports or incorrect aliasing. -
SyntaxError: Cannot use import statement outside a module
cause Attempting to use ES module `import` syntax in a Node.js environment configured for CommonJS (`require()`) without proper module configuration.fixTo use ESM, add `'type': 'module'` to your `package.json` file, or save your script files with a `.mjs` extension. Alternatively, if you need to load ESM in a CJS context, use dynamic import: `const { highlight } = await import('cli-high');`. -
Error: ENOENT: no such file or directory, open './path/to/your/js/file'
cause When using `cli-high` via its CLI (`npx cli-high ./path/to/file`), this error indicates that the provided file path does not exist or is incorrect relative to where the command is executed.fixDouble-check the file path provided to the `npx cli-high` command. Ensure the file exists and the path is either absolute or correct relative to your current working directory.
Warnings
- breaking As a 0.x.x version, cli-high's API is subject to breaking changes in minor or even patch releases without strict adherence to SemVer. The API surface may evolve rapidly.
- gotcha Mixing CommonJS `require()` with cli-high's presumed ESM-first approach (indicated by TypeScript usage and import examples) can lead to import errors like 'TypeError: highlight is not a function'.
Install
-
npm install cli-high -
yarn add cli-high -
pnpm add cli-high
Imports
- highlight
const highlight = require('cli-high')import { highlight } from 'cli-high' - Options
import type { Options } from 'cli-high' - Language
import type { Language } from 'cli-high'
Quickstart
import { highlight, type Options } from 'cli-high';
const javascriptCode = `
function greet(name: string) {
console.log("Hello, " + name + "!");
}
greet("World");
`;
const options: Options = {
language: 'js', // Or 'typescript', 'json', etc.
theme: 'dark' // Or 'light'
};
const highlightedCode = highlight(javascriptCode, options);
console.log(highlightedCode);
// Example of highlighting a different language
const jsonConfig = `{
"name": "cli-high-project",
"version": "1.0.0",
"scripts": {
"start": "node index.js"
}
}`;
const highlightedJson = highlight(jsonConfig, { language: 'json' });
console.log('\n' + highlightedJson);