Detect whether a terminal supports color
npx @tessl/cli install tessl/npm-supports-color@9.4.0supports-color is a lightweight JavaScript library that detects whether a terminal supports color output. It provides intelligent color support detection across different environments including Node.js and browsers, analyzing terminal streams to determine color capabilities ranging from basic 16-color support to full truecolor (16 million colors).
npm install supports-colorimport supportsColor from "supports-color";For the createSupportsColor function:
import supportsColor, { createSupportsColor } from "supports-color";For CommonJS:
const supportsColor = require("supports-color");
const { createSupportsColor } = require("supports-color");import supportsColor from "supports-color";
// Check if terminal supports color
if (supportsColor.stdout) {
console.log("Terminal stdout supports color");
console.log(`Color level: ${supportsColor.stdout.level}`);
}
// Check specific color capabilities
if (supportsColor.stdout.has256) {
console.log("Terminal supports 256 colors");
}
if (supportsColor.stderr.has16m) {
console.log("Terminal stderr supports 16 million colors (truecolor)");
}
// Custom stream analysis
import { createSupportsColor } from "supports-color";
const customSupport = createSupportsColor(process.stdout);
if (customSupport) {
console.log(`Custom stream color level: ${customSupport.level}`);
}The default export provides pre-analyzed color support for standard streams.
interface SupportsColor {
stdout: ColorInfo;
stderr: ColorInfo;
}
const supportsColor: SupportsColor;Properties:
stdout: ColorInfo - Color support information for stdout streamstderr: ColorInfo - Color support information for stderr streamCreates color support analysis for arbitrary streams with optional configuration.
/**
* Analyze color support for a custom stream
* @param stream - Optional WriteStream to analyze (defaults to stdout-like behavior)
* @param options - Optional configuration options
* @returns Color support information or false if no color support
*/
function createSupportsColor(stream?: import('node:tty').WriteStream, options?: Options): ColorInfo;Parameters:
stream (optional): import('node:tty').WriteStream - The stream to analyze for color supportoptions (optional): Options - Configuration options for the analysisReturns: ColorInfo - Color support information or false
Usage Examples:
import { createSupportsColor } from "supports-color";
// Analyze stdout with default settings
const stdoutSupport = createSupportsColor(process.stdout);
// Analyze stderr without flag sniffing
const stderrSupport = createSupportsColor(process.stderr, { sniffFlags: false });
// Analyze without providing a stream (uses default behavior)
const defaultSupport = createSupportsColor();/**
* Color support information - either a ColorSupport object or false
*/
type ColorInfo = ColorSupport | false;/**
* Object describing terminal color capabilities
*/
interface ColorSupport {
/** The color support level (0-3) */
level: ColorSupportLevel;
/** Whether basic 16 colors are supported */
hasBasic: boolean;
/** Whether ANSI 256 colors are supported */
has256: boolean;
/** Whether Truecolor 16 million colors are supported */
has16m: boolean;
}/**
* Numeric representation of color support levels
* - 0: All colors disabled
* - 1: Basic 16 colors support
* - 2: ANSI 256 colors support
* - 3: Truecolor 16 million colors support
*/
type ColorSupportLevel = 0 | 1 | 2 | 3;/**
* Configuration options for createSupportsColor function
*/
interface Options {
/** Whether process.argv should be sniffed for --color and --no-color flags (default: true) */
readonly sniffFlags?: boolean;
}The library responds to various environment variables for controlling color behavior:
'0' or 'false': Force disable colors'1' or 'true': Force basic color support'2': Force 256 color support'3': Force truecolor support'xterm-256color', 'dumb')'truecolor')The library automatically detects and responds to command-line flags (when sniffFlags is true):
--color, --colors, --color=true, --color=always--no-color, --no-colors, --color=false, --color=never--color=256--color=16m, --color=full, --color=truecolorThe library is designed to gracefully handle various environments and never throws errors. It returns false when color support cannot be determined or is explicitly disabled.