Chalk for JavaScript (Node.js): Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

What is Chalk and why is it needed?

\n\n

Chalk is one of the most popular libraries for Node.js, designed for styling terminal (console) output. If you have ever written console.log() and wanted the text to be colored, bold, underlined, or blinking — Chalk solves this problem elegantly and simply.

\n\n

Why is this needed? In real projects, colored output helps:

\n
    \n
  • Visually separate logs (errors in red, warnings in yellow, success in green).
  • \n
  • Improve UX of CLI utilities and scripts.
  • \n
  • Draw attention to important messages.
  • \n
  • Make output more readable in long logs.
  • \n
\n\n

Chalk has no dependencies, works fast, and supports method chaining. It automatically detects whether the terminal supports colors and disables styling if necessary (for example, when redirecting output to a file).

\n\n

Installation

\n\n

Chalk is distributed via npm. Install it in your project:

\n\n
npm install chalk
\n\n

If you are using yarn:

\n\n
yarn add chalk
\n\n

After installation, import the library into your script:

\n\n
const chalk = require('chalk');
\n\n

For projects using ES modules (type: "module" in package.json), the import looks like this:

\n\n
import chalk from 'chalk';
\n\n

Important: Starting from Chalk version 5.0, the library became ESM-only. If you are using CommonJS (require), install version 4.x:

\n\n
npm install chalk@4.1.2
\n\n

Quick Start

\n\n

Let's try the first example. Create a file example.js and write:

\n\n
const chalk = require('chalk');\n\nconsole.log(chalk.blue('Hello, world!'));\nconsole.log(chalk.red.bold('Error!'));\nconsole.log(chalk.green.underline('Success'));\nconsole.log(chalk.yellow.bgBlue('Yellow text on blue background'));
\n\n

Run it:

\n\n
node example.js
\n\n

You will see colored output. If colors are not displayed, check if your terminal supports ANSI colors (modern terminals do).

\n\n

Main Methods and Functions

\n\n

Chalk provides two types of API: modifier methods (text styles) and color methods (text and background colors). All of them can be combined through method chaining.

\n\n

1. chalk.red() — red text color

\n

What it does: Colors the text red. Most often used for error messages.

\n

Parameters: Accepts a string or multiple strings (separated by commas, they will be joined).

\n
console.log(chalk.red('Error: file not found'));\nconsole.log(chalk.red('Critical', 'error', 'system')); // joins with space
\n\n

2. chalk.green() — green text color

\n

What it does: Colors the text green. Used for success messages.

\n

Parameters: Similar to red.

\n
console.log(chalk.green('Operation completed successfully'));\nconsole.log(chalk.green('✓', 'Done'));
\n\n

3. chalk.blue() — blue text color

\n

What it does: Colors the text blue. Often used for informational messages.

\n

Parameters: A string or multiple strings.

\n
console.log(chalk.blue('Info: version 2.0.1'));
\n\n

4. chalk.yellow() — yellow text color

\n

What it does: Colors the text yellow. Traditionally used for warnings.

\n

Parameters: A string or multiple strings.

\n
console.log(chalk.yellow(                    

Recommendations