The fastest Node.js library for formatting terminal text with ANSI colors~!
kleur is a lightweight, zero-dependency library designed for formatting terminal text with ANSI colors in Node.js applications. It provides a simple chainable API for applying colors, background colors, and text modifiers like bold, underline, and italic to console output. With over 47 million weekly downloads, kleur has become a standard choice for CLI tools, build systems, and developer tooling that requires fast, reliable terminal styling.
The library was created as a performance-focused alternative to existing color libraries, optimizing for speed without sacrificing functionality. It uses modern JavaScript patterns and supports both CommonJS and ES modules, making it compatible with contemporary bundlers and build tools. kleur automatically detects terminal capabilities through environment variables and TTY checks, disabling colors when output is piped or terminals don't support ANSI codes.
What sets kleur apart is its emphasis on bundle size and tree-shaking. The library offers a modular 'kleur/colors' export that allows developers to import only the specific color functions they need, reducing bundle size in frontend tooling and CLI applications. Unlike some alternatives, kleur doesn't modify global prototypes or String.prototype, maintaining a clean, functional API that's predictable and side-effect free.
Developers working on command-line tools, build systems, test runners, or any Node.js application that outputs to the terminal will find kleur invaluable. Its API is intuitive enough for quick integration while being performant enough for high-throughput logging scenarios where color formatting happens thousands of times per second.
import kleur from 'kleur';
import { red, green, bold, yellow } from 'kleur/colors';
function logBuildResults(errors, warnings, successCount) {
console.log(kleur.bold().underline('Build Results'));
console.log('');
if (errors.length > 0) {
console.log(kleur.red().bold(`✗ ${errors.length} errors:`));
errors.forEach(err => {
console.log(kleur.red(` - ${err.file}: ${err.message}`));
});
}
if (warnings.length > 0) {
console.log(kleur.yellow(`⚠ ${warnings.length} warnings:`));
warnings.forEach(warn => {
console.log(kleur.yellow(` - ${warn.message}`));
});
}
console.log(kleur.green(`✓ ${successCount} files compiled successfully`));
const nested = kleur.blue(
`Total time: ${kleur.bold().white('1.2s')}`
);
console.log(nested);
}
const mockErrors = [
{ file: 'src/index.js', message: 'Unexpected token' }
];
const mockWarnings = [
{ message: 'Unused variable "temp"' }
];
logBuildResults(mockErrors, mockWarnings, 42);
if (process.env.NO_COLOR || !process.stdout.isTTY) {
kleur.enabled = false;
}CLI Application Output: Building command-line tools that need to display success messages in green, errors in red, and warnings in yellow. kleur's chainable API makes it easy to create consistent, readable terminal output with minimal code overhead.
Build Tool Logging: Integration into webpack plugins, Vite plugins, or Rollup configurations where fast color formatting is critical during watch mode. The zero-dependency footprint prevents dependency bloat in developer tooling.
Test Runner Output: Implementing custom test reporters that color-code passing tests (green), failures (red), and skipped tests (yellow/gray). kleur's performance shines when formatting hundreds or thousands of test results rapidly.
Development Server Feedback: Creating colored console output for development servers that need to log requests, compilation status, and error messages. The automatic color disabling when output is piped ensures clean logs in CI/CD environments.
Log Processing Scripts: Writing Node.js scripts that parse and colorize log files for better readability during debugging sessions. The tree-shakeable API keeps utility scripts lightweight when bundled or deployed.
npm install kleurpnpm add kleurbun add kleur