CLI Styles
cli-styles is a lightweight utility library designed to standardize the visual appearance of command-line interface (CLI) applications, particularly in relation to user prompts. Instead of being a prompt solution itself, it provides a consistent set of styles for user input (e.g., default, password, invisible), status indicators (question, success, error marks), and prompt delimiters (e.g., '›'). This enables different prompt libraries to adopt a uniform look and feel. The package is currently at version 1.0.0, released in February 2020, and does not appear to be under active development, maintaining a stable API. It is frequently used in conjunction with `prompt-skeleton` and a suite of dedicated prompt libraries like `text-prompt` and `select-prompt` to ensure visual coherence across an application's CLI interactions. Its primary differentiator is its focus on providing reusable styling primitives rather than a full-fledged prompt system.
Common errors
-
ReferenceError: require is not defined
cause Attempting to use `require('cli-styles')` in an ECMAScript Module (ESM) context (e.g., in a `.mjs` file or a module with `"type": "module"` in `package.json`).fixFor ESM, use `import cliStyles from 'cli-styles';` to import the default export. For CommonJS, ensure your file is `.cjs` or `"type": "commonjs"` is specified, or simply use the default Node.js behavior. -
TypeError: cliStyles.input.default is not a function
cause This error is unlikely to occur unless `cliStyles` itself is not correctly imported or is `undefined`. More commonly, `cliStyles.input` might be treated as a function if `cliStyles` isn't properly destructured or imported as a default.fixEnsure `cliStyles` is imported correctly (e.g., `const cliStyles = require('cli-styles')` or `import cliStyles from 'cli-styles'`). The `input.default` property is a function provided by `chalk`, so verify `chalk` is also correctly resolved within `cli-styles`. -
TypeError: Cannot read properties of undefined (reading 'default') at Object.input
cause This error often happens if you've attempted named destructuring like `import { input } from 'cli-styles'` in an ESM context. Since `cli-styles` only has a default export, `input` would be `undefined`.fixImport the entire default export: `import cliStyles from 'cli-styles'`. Then access its properties: `cliStyles.input.default(...)`.
Warnings
- gotcha cli-styles is a CommonJS module and does not natively support named ESM imports. Attempting `import { input } from 'cli-styles'` will fail or result in `undefined` values in pure ESM environments.
- deprecated The package is not actively maintained since its last update in February 2020. While stable at 1.0.0, users should be aware that new features, bug fixes, or updates for compatibility with newer Node.js versions or terminal features are unlikely.
- gotcha cli-styles is not a prompt library itself; it provides styling primitives for existing prompt implementations. Directly calling `cliStyles.input` or `cliStyles.indicator` will not render a prompt or text, as these are objects containing styling functions or pre-styled strings.
- gotcha The package depends on older versions of `chalk` (implicitly via its `package.json` at the time of 1.0.0 release). This might lead to dependency conflicts if your project uses a much newer `chalk` version, or it might not leverage the latest color capabilities or performance improvements.
Install
-
npm install cli-styles -
yarn add cli-styles -
pnpm add cli-styles
Imports
- cliStyles
import { input, indicator, delimiter } from 'cli-styles'import cliStyles from 'cli-styles'
- cliStyles (CommonJS)
const cliStyles = require('cli-styles') - input.default
cliStyles.input('Your input')cliStyles.input.default('Your input')
Quickstart
import cliStyles from 'cli-styles';
import readline from 'readline';
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
async function runPrompt() {
// Using the question indicator and delimiter
process.stdout.write(cliStyles.indicator.question + ' ' + cliStyles.delimiter + ' ');
// Using default input style
const question = cliStyles.input.default('What is your name? ');
rl.question(question, (name) => {
if (name.length > 0) {
process.stdout.write(cliStyles.indicator.success + ' ' + cliStyles.delimiter + ' ');
console.log(cliStyles.input.default(`Hello, ${name}!`));
} else {
process.stdout.write(cliStyles.indicator.error + ' ' + cliStyles.delimiter + ' ');
console.log(cliStyles.input.default('Name cannot be empty.'));
}
rl.close();
});
}
runPrompt();