How to change node.js's console font color?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In Node.js you usually do not change the terminal's actual font. What you can control is the text color and style emitted to the terminal, typically by writing ANSI escape sequences directly or by using a small library that wraps them.
Use ANSI Escape Codes Directly
The terminal understands ANSI escape sequences for color and formatting. That means a plain console.log call can print colored text without any dependency.
The \x1b[0m sequence resets the style back to the terminal default. Without it, later output may keep the same color.
A few common foreground codes are:
- '
31red' - '
32green' - '
33yellow' - '
34blue' - '
35magenta' - '
36cyan'
This is the lightest-weight solution and works well for quick scripts.
Use a Library for Cleaner Code
For production code, raw escape codes become noisy. A formatting library makes logs easier to read and maintain.
A lightweight option is picocolors:
Example usage:
If your project uses CommonJS:
Libraries like this also handle nested styles more cleanly than manual escape sequences.
Combine Color With Other Emphasis
Terminal styling is not limited to color. You can also add bold, dim, underline, or background colors if the terminal supports them.
Using raw ANSI:
Using a library:
This is useful for differentiating errors, warnings, and headings in CLI tools.
Remember That Support Depends on the Terminal
Node prints sequences to stdout or stderr, but the terminal decides how to render them. Most modern terminals support ANSI colors, but behavior can differ when output is:
- redirected to a file
- viewed in a minimal CI console
- consumed by another process
- displayed in an older shell environment
If your script may run in many environments, detect whether color should be enabled or provide a --no-color option.
Use console.error and console.warn Intentionally
Color can improve log clarity, but it should reinforce meaning rather than replace it. Use the right output stream too.
This keeps terminal color aligned with semantics and plays better with shell pipelines.
Common Pitfalls
- Expecting Node.js to change the terminal's actual font is a category mistake. It can style output, not reconfigure the terminal application itself.
- Forgetting the reset sequence when using raw ANSI codes causes later log lines to inherit the same style.
- Hardcoding colorful output without considering redirected logs can make files harder to read.
- Using raw escape codes everywhere makes maintenance harder once the CLI grows beyond a few lines.
- Relying only on color to convey severity is an accessibility problem; combine color with good wording or symbols.
Summary
- In Node.js, changing console color means styling terminal output, usually with ANSI escape codes.
- Raw ANSI sequences are enough for quick scripts and simple logging.
- A library such as
picocolorsmakes colored output cleaner and easier to maintain. - Terminal support varies, so color should be treated as a presentation layer, not a guarantee.
- Use color to reinforce meaning, not to replace clear log structure.

