A terminal full of plain white text makes it hard to tell an error apart from a routine log line at a glance. Coloring your output fixes that, and Node.js supports it two ways.
Option 1: ANSI escape codes, no library needed
Terminals understand special character sequences that change text color. You can use them directly:
const red = '\x1b[31m';
const green = '\x1b[32m';
const yellow = '\x1b[33m';
const reset = '\x1b[0m';
console.log(green + 'Payment succeeded' + reset);
console.log(yellow + 'Retrying connection...' + reset);
console.log(red + 'Payment failed' + reset);
Each code switches the color on; reset switches it back off, which matters — forget it, and every line after stays that color.
A few common codes worth keeping handy
| Code | Color |
|---|---|
\x1b[31m | Red |
\x1b[32m | Green |
\x1b[33m | Yellow |
\x1b[34m | Blue |
\x1b[36m | Cyan |
\x1b[0m | Reset |
This works with zero dependencies, but it's easy to forget a reset, and the codes themselves aren't exactly self-documenting when you're reading them back later.
Option 2: the chalk library
npm install chalk
import chalk from 'chalk';
console.log(chalk.green('Payment succeeded'));
console.log(chalk.yellow('Retrying connection...'));
console.log(chalk.red.bold('Payment failed'));
chalk wraps the same escape codes in a readable API, resets color automatically after each call, and supports chaining styles like .bold, .underline, and background colors (chalk.bgRed.white(...)).
Where this is actually useful
- Logging severity — green for success, yellow for warnings, red for errors, so problems jump out visually during development.
- CLI tools — command-line utilities built with Node.js commonly use color to separate prompts, results, and errors.
- Debugging output — coloring one specific variable you're tracking down makes it easy to spot among a wall of other log lines.
A word of caution: not every environment renders these colors — some CI/CD logging systems and certain log aggregators show the raw escape codes as garbled text instead of color. It's worth checking how your logs actually get viewed in production before leaning on color there, and many teams disable it outside of local development for exactly this reason.