Configs2026年4月25日·1 分钟阅读

chalk — Terminal String Styling for Node.js

chalk is the most popular library for adding color and style to terminal output in Node.js, with a clean chainable API and automatic color support detection.

assetLangBanner.body

Introduction

chalk provides a clean, chainable API for styling terminal strings with colors, backgrounds, and text decorations. It is the standard choice for CLI output formatting in the Node.js ecosystem, used by thousands of packages.

What chalk Does

  • Applies foreground and background colors to terminal text
  • Supports bold, italic, underline, strikethrough, and dim modifiers
  • Detects terminal color support level automatically (16, 256, or truecolor)
  • Allows hex, RGB, and ANSI-256 color values
  • Provides tagged template literal support for inline styling

Architecture Overview

chalk wraps ANSI escape codes behind a chainable property-based API. When you write chalk.red.bold(text), each property access builds a style stack, and the final function call wraps the text in the corresponding ANSI open/close sequences. chalk v5+ is a pure ESM module with no dependencies, relying on a small internal library for color-level detection based on environment variables and terminal capabilities.

Self-Hosting & Configuration

  • Install via npm install chalk (v5+ is ESM-only; use v4 for CommonJS)
  • Set the FORCE_COLOR environment variable to override color detection (0=off, 1/2/3=levels)
  • Use chalk.level programmatically to force a specific color depth
  • Import chalk directly; no initialization or configuration object is required
  • For tagged template literals, import chalk and use chalk`{red text} syntax

Key Features

  • Chainable API that reads like natural language: chalk.green.underline('success')
  • Automatic detection of terminal color capabilities (no manual config needed)
  • Supports 16.7 million colors via hex and RGB: chalk.hex('#DEADED')
  • Nestable styles so inner styles override outer ones correctly
  • Zero dependencies in v5+ for minimal install footprint

Comparison with Similar Tools

  • kleur — smaller and faster for basic 16-color use; chalk supports 256 and truecolor and has a richer API
  • picocolors — ultra-lightweight with minimal API; chalk offers chaining, hex/RGB, and template literals
  • ansi-colors — similar API to chalk without dependencies; chalk has broader ecosystem adoption
  • colorette — fast and tiny; chalk provides more granular color-level control and tagged templates
  • Rich (Python) — full terminal rendering library; chalk focuses solely on string styling for Node.js

FAQ

Q: Why does chalk v5 require ESM? A: chalk v5 dropped CommonJS to align with the Node.js ecosystem's move toward ES modules. Use chalk v4 if your project must use require().

Q: How do I disable colors in CI or logs? A: Set FORCE_COLOR=0 or NO_COLOR=1 environment variable. chalk respects the NO_COLOR convention.

Q: Can chalk style template literals? A: Yes. chalk supports tagged template literals for inline formatting: chalk`Hello {red world}` .

Q: Does chalk work in the browser? A: No. chalk is designed for Node.js terminals. For browser console styling, use the %c CSS format specifier directly.

Sources

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产