Print colored terminal output in Node.js

Use ANSI sequences or Chalk, reset styles, and adapt color output to the destination instead of assuming every terminal or log file supports it.

Best practices
Illustration of an event loop and asynchronous tasks

A terminal that understands ANSI styling can render text between a color sequence and a reset sequence. Node writes those characters; the terminal decides how to display them.

Use ANSI sequences

const red = '\x1b[31m';
const green = '\x1b[32m';
const reset = '\x1b[0m';

console.log(red + 'Operation failed' + reset);
console.log(green + 'Completed' + reset);

Reset the style after the message so later output does not inherit it.

Color Foreground Background
Black \x1b[30m \x1b[40m
Red \x1b[31m \x1b[41m
Green \x1b[32m \x1b[42m
Yellow \x1b[33m \x1b[43m
Blue \x1b[34m \x1b[44m
Magenta \x1b[35m \x1b[45m
Cyan \x1b[36m \x1b[46m
White \x1b[37m \x1b[47m

Common style sequences include \x1b[1m for bold or increased intensity, \x1b[4m for underline, and \x1b[7m for inverse colors. You can combine them, for example \x1b[1m\x1b[33m for bold yellow. Appearance depends on the terminal and theme.

Use Chalk for readable styling

Install it with npm install chalk. Chalk 5 is an ES module package, so use an ES module such as colors.mjs:

import chalk from 'chalk';

console.log(chalk.red('Red text'));
console.log(chalk.green.bold('Bold green text'));
console.log(chalk.bgBlue.white('White text on blue'));

Chalk detects color support and normally disables styles for destinations without it. Explicit configuration or environment overrides can change that behavior, so “redirecting always removes colors” is too strong. Check package compatibility if your application uses older CommonJS tooling.

Keep redirected logs readable

A basic manual policy is to color only a terminal destination:

const message = 'Operation failed';
console.log(process.stdout.isTTY ? `\x1b[31m${message}\x1b[0m` : message);

isTTY is a useful signal, not a complete color-capability test. Respect your CLI's color preference and use a library's capability detection when you support many terminals. Escapes written to a file are control characters, not necessarily an encoding problem.

FAQ

Does this work on Windows? Modern Windows terminals commonly support ANSI styling, but behavior depends on the console host, configuration, and output destination. Test the terminals you support rather than relying on an operating-system version alone.

Should production logs be colored? Keep machine-consumed output plain or structured, usually JSON. Color can help an interactive console, while the file or collector output should remain easy to parse.

Why did later output change color too? The style was not reset, or a terminal interpreted a sequence differently. End each styled message deliberately and avoid injecting arbitrary terminal control characters from untrusted input.

References

Get a tailored plan

Contact us

Join the community

Scan with WeChat
to join the community

Try Guance

Start online and pay only for what you use.

Get started

Choose a Guance plan

Code hosting