How to colorize console output in Node.js
Colorized terminal output makes CLI tools more readable by visually distinguishing success, errors, and warnings.
As the creator of CoreUI with over 10 years of Node.js experience since 2014, I’ve added color output to build scripts, test runners, and deployment tools to make status immediately clear.
The standard approach uses the chalk library which handles ANSI escape codes across platforms including Windows.
This produces professional-looking terminal output.
Install chalk and use basic colors.
npm install chalk
const chalk = require('chalk')
console.log(chalk.green('✓ Build succeeded'))
console.log(chalk.red('✗ Build failed'))
console.log(chalk.yellow('⚠ Warning: deprecated API'))
console.log(chalk.blue('ℹ Running tests...'))
console.log(chalk.gray('Skipping optional step'))
chalk.green(), chalk.red(), etc. wrap text in ANSI codes. The terminal renders them as colors. chalk detects whether the terminal supports colors and falls back gracefully.
Combining Styles
Chain multiple styles together.
const chalk = require('chalk')
console.log(chalk.bold.green('SUCCESS'))
console.log(chalk.bold.red('ERROR'))
console.log(chalk.dim.italic('debug info'))
console.log(chalk.bgRed.white.bold(' FAILED '))
console.log(chalk.underline('https://coreui.io'))
Styles chain with dots. bold, italic, dim, underline modify text weight and decoration. bgRed sets background color. Combine foreground color, background, and text decoration freely.
Creating a Logger Utility
Build a reusable logging helper.
const chalk = require('chalk')
const log = {
success: (msg) => console.log(chalk.green('✓'), msg),
error: (msg) => console.error(chalk.red('✗'), msg),
warn: (msg) => console.warn(chalk.yellow('⚠'), msg),
info: (msg) => console.log(chalk.blue('ℹ'), msg),
debug: (msg) => {
if (process.env.DEBUG) {
console.log(chalk.gray(`[debug] ${msg}`))
}
},
step: (n, total, msg) => console.log(
chalk.cyan(`[${n}/${total}]`), msg
)
}
log.info('Starting build...')
log.step(1, 3, 'Compiling TypeScript')
log.step(2, 3, 'Bundling assets')
log.step(3, 3, 'Generating docs')
log.success('Build complete in 4.2s')
The logger object standardizes output format. The debug method checks an environment variable for conditional output. The step helper shows progress through multi-stage operations.
Using Without Dependencies
ANSI codes work without any library.
const RESET = '\x1b[0m'
const GREEN = '\x1b[32m'
const RED = '\x1b[31m'
const YELLOW = '\x1b[33m'
const BOLD = '\x1b[1m'
console.log(`${GREEN}✓ Done${RESET}`)
console.log(`${RED}${BOLD}Error!${RESET}`)
console.log(`${YELLOW}Warning${RESET}`)
ANSI escape codes work in most terminals without npm packages. Use \x1b[0m to reset after every colored segment. This is useful for zero-dependency scripts or environments where npm isn’t available.
Checking Terminal Color Support
Disable colors when output is piped.
const chalk = require('chalk')
if (!chalk.level) {
console.log('Terminal does not support colors')
}
const isTTY = process.stdout.isTTY
const status = isTTY ? chalk.green('OK') : 'OK'
console.log(status)
chalk.level is 0 when colors aren’t supported. process.stdout.isTTY is true in interactive terminals and false when output is piped to a file. Skip colors when piped to avoid ANSI codes in log files.
Best Practice Note
This is the same console output approach we use in CoreUI build and development tools. Always reset color after every segment to prevent color bleeding. Respect NO_COLOR environment variable convention - chalk handles this automatically. For CI environments, test that colored output doesn’t break log parsers. The chalk library is by far the most popular approach and handles all cross-platform edge cases including Windows command prompt and PowerShell. For applications that both log to console and write to files, strip ANSI codes from file output using the strip-ansi package.



