Skip to content

Replacements for chalk ​

styleText (native) ​

Since Node 20.x, you can use the styleText function from the node:util module to style text in the terminal.

TIP

Node.js provides a codemod which can migrate some of this automatically, see the docs at @nodejs/chalk-to-util-styletext for more details.

Example:

ts
import { styleText } from 'node:util'
import chalk from 'chalk'

console.log(`Hello ${chalk.blue('blue')} world!`) 
console.log(`Hello ${styleText('blue', 'blue')} world!`) 
console.log(`I am ${chalk.hex('#ec8f5e')('hex color')}!`) 
console.log(`I am ${styleText('#ec8f5e', 'hex color')}!`) 

When using multiple styles, you can pass an array to styleText:

ts
console.log(`I am ${chalk.blue.bgRed('blue on red')}!`) 
console.log(`I am ${styleText(['blue', 'bgRed'], 'blue on red')}!`) 

NOTE

Before Node v26.1.0, styleText did not support hex colors or RGB colors (e.g. #EFEFEF or 255, 239, 235). Hex colors are supported starting in v26.1.0, but RGB colors are still not supported. You can view the available styles in the Node documentation.

picocolors ​

picocolors follows a similar API but without chaining:

ts
import chalk from 'chalk'
import picocolors from 'picocolors'

console.log(`Hello ${chalk.blue('blue')} world!`) 
console.log(`Hello ${picocolors.blue('blue')} world!`) 

// A chained example
console.log(chalk.blue.bgRed('blue on red')) 
console.log(picocolors.blue(picocolors.bgRed('blue on red'))) 

NOTE

picocolors currently does not support RGB and hex colors (e.g. #EFEFEF or 255, 239, 235).

ansis ​

ansis supports a chaining syntax similar to chalk and supports both RGB, and hex colors.

Example:

ts
import ansis from 'ansis'
import chalk from 'chalk'

console.log(`Hello ${chalk.blue('blue')} world!`) 
console.log(`Hello ${ansis.blue('blue')} world!`) 

When using multiple styles, you can chain them just like in chalk:

ts
console.log(chalk.blue.bgRed('blue on red')) 
console.log(ansis.blue.bgRed('blue on red')) 

Similarly, you can use RGB and hex colors:

ts
console.log(chalk.rgb(239, 239, 239)('Hello world!')) 
console.log(ansis.rgb(239, 239, 239)('Hello world!')) 

Browser support ​

While these libraries are primarily designed for terminal output, some projects may need colored output in browser environments.

Following MDN documentation, the native approach is %c directive in console.log:

ts
console.log(
  'Hello %ce%c1%c8%ce',
  'color: #ec8f5e;',
  'color: #f2ca60;',
  'color: #bece57;',
  'color: #7bb560;',
  'Ecosystem Performance'
)

Library support:

  • ansis - colors are supported in Chromium browsers
  • picocolors - strips colors in browser environments
  • node:util - is not available in browsers

Released under the MIT License. (cb509db0)