Skip to content
Theme:

Node.js added utility for text formatting — you may not need chalk anymore

Node.js 21.7.0 comes with a new text formatting utility. It is simple to use but powerful because modifiers can be combined. You may not need chalk anymore. Let’s have a look at how it works.

import { styleText } from "node:util";

const exampleRed = styleText("red", "I am red!");
const exampleBlue = styleText("blue", "I am blue!");
const exampleBlueBold = styleText("bold", exampleBlue);

console.log(exampleRed);
console.log(exampleBlue);
console.log(exampleBlueBold);
Example of the simple use of node:util styleText

Nice, right? Here is a handy list of all the available modifiers divided into general, foreground and background colours.

  • modifiers: reset, bold, italic, underline, strikethrough, hidden, dim, overlined, blink, inverse, doubleunderline and framed
  • foreground: black, red, green, yellow, blue, magenta, cyan, white, gray, redBright, greenBright, yellowBright, blueBright, magentaBright, cyanBright and whiteBright
  • background: bgBlack, bgRed, bgGreen, bgYellow, bgBlue, bgMagenta, bgCyan, bgWhite, bgGray, bgRedBright, bgGreenBright, bgYellowBright, bgBlueBright, bgMagentaBright, bgCyanBright, bgWhiteBright

Node v20.13.0 release added an ability to pass an array of modifiers, like util.styleText(['underline', 'italic'], 'Hi'). Super handy!

Leave a comment

👆 you can use Markdown here

Your comment is awaiting moderation. Thanks!