CLI Width Utility
cli-width is a JavaScript utility for Node.js environments that accurately determines the current width of the stdout window. It employs a robust fallback mechanism, checking `tty` information, `output.columns` property, a custom `CLI_WIDTH` environment variable, and finally a configurable default width. The package is currently stable at version 4.1.0 and is actively maintained, with releases occurring as needed for bug fixes or minor enhancements. It is designed to be lightweight and provides TypeScript types out-of-the-box, making it suitable for modern Node.js projects that require responsive command-line interfaces. Its primary differentiator is the comprehensive fallback strategy to ensure a width is always returned.
Common errors
-
TypeError: cliWidth is not a function
cause Attempting to call `cliWidth` after importing it incorrectly using named import syntax when it's a default export.fixChange `import { cliWidth } from 'cli-width';` to `import cliWidth from 'cli-width';` or `const cliWidth = require('cli-width');`. -
ReferenceError: require is not defined (in ESM context)
cause Using `require('cli-width')` in an ECMAScript Module (ESM) file without proper configuration or transpilation.fixIf your project is configured for ESM (e.g., `"type": "module"` in `package.json`), use `import cliWidth from 'cli-width';`. If you need to use `require` in an ESM file, consider dynamic `import()` or review your build setup.
Warnings
- breaking The way options are passed to `cliWidth` changed significantly in v2.0.0. Previously, options might have been positional or less structured. Now, all configuration must be passed via a single `options` object as the first argument.
- gotcha When running in non-TTY environments (e.g., piped output, CI/CD without TTY emulation), `cli-width` might return the `defaultWidth` or `0` if not explicitly configured. This is expected behavior as there's no terminal to query for dimensions.
- gotcha The `tty` module fallback relies on `require('tty')`. In environments where the `tty` module is not available or behaves unexpectedly (e.g., some browser bundlers trying to shim Node built-ins), this fallback might fail. Ensure your target environment is Node.js.
Install
-
npm install cli-width -
yarn add cli-width -
pnpm add cli-width
Imports
- cliWidth
import { cliWidth } from 'cli-width';import cliWidth from 'cli-width';
- cliWidth (CommonJS)
const cliWidth = require('cli-width'); - Options type
import type { Options } from 'cli-width';
Quickstart
import cliWidth, { Options } from 'cli-width';
import * as process from 'process';
// Get the current CLI width with default fallbacks
const currentWidth = cliWidth();
console.log(`Current CLI width: ${currentWidth}`);
// Define custom options for fallback behavior
const customOptions: Options = {
defaultWidth: process.env.DEFAULT_CLI_WIDTH ? parseInt(process.env.DEFAULT_CLI_WIDTH, 10) : 80,
output: process.stdout,
tty: require('tty'), // Use the built-in tty module
};
// Get CLI width with custom options
const customWidth = cliWidth(customOptions);
console.log(`CLI width with custom options: ${customWidth}`);
// Example of setting an environment variable to test CLI_WIDTH fallback
process.env.CLI_WIDTH = '120';
const envVarWidth = cliWidth();
console.log(`CLI width with CLI_WIDTH env var: ${envVarWidth}`);
// Clean up environment variable for subsequent tests
delete process.env.CLI_WIDTH;