todor's blog

Back

native terminal output styling with nodejs

Posted at 08, Aug, 2024

Overview

Spending an unnecessarily long time on twitter, I am of course up to date with the latest and greatest in the JavaScript ecosystem.

For example, I had seen that node had shipped native support for color printing in a recent version. But when I was hacking together a little script, and wanted to add some color to the output (because why not), I had to spend an unusually long time, searching how to do it without installing a dependency. After exhausting every combination of search terms I could think of, I eventually found what I was looking for but it struck me as odd that it was as difficult as it was. nodejs’s docs did not help either as they are notoriously difficult to search through (at least for me).

That’s why I decided to put together this short blog post, in hopes it saves someone a little time.

tl;dr

import { styleText } from 'node:util';
console.log(styleText(['bold', 'red'], 'hey'));

The options

Since version 20.12.0, node has native support for styling text.

As of the time of writing, the styleText module is marked as Stability: 1.1 - Active development.

In terms of packages, the most popular ones seem to be chalk, and picocolors, with notable mentions for colors, and cli-colors.

In this blog we will be comparing node’s styling options with chalk and picocolors, as the most used ones.

Size

chalk comes in at 44kb, while picocolors is 5.2kb. Neither package has external dependencies, which is always nice.

Of course styleText has no cost as it comes bundled with node.

Features

Colors

In chalk, you can pass a color name, a hex value, or rbg values. In general, chalk offers support for 256 colors, plus TrueColor support.

In picocolors and styleText you are limited to choosing from a predefined set of colors.

Text Decoration

All of chalk, picocolors, and styleText offer the option to underline, bold, strikethrough, and many more.

Other Features

picocolors obeys the NO_COLOR env variable, while chalk seems to look at the --colors/--no-colors cli flags and has it’s own way to disable colors.

As of the time of writing, node’s styleText does not obey neither the --no-colors flag, nor the NO_COLORS environment variable.

Conclusion

chalk is used by more than 100k other packages so chances are you already have it as a dependency in your dependency tree. It is obviously on top in terms of features and maturity but adds a few extra kilobytes on each install. 44kb might seem negligible but they might quickly stack up. picocolors offers a similar feature set at a smaller space cost and is gaining more and more traction, especially around the e18e initiative that aims to cleanup the JavaScript ecosystem, among other things.

What about node’s styleText? It doesn’t have all the bells and whistles of chalk and picocolors but I think it has enough. It’s biggest advantage is that no packages is required to cover the majority of use cases (color printing), so maybe consider it for your next script, before reaching for a package.

Last modified at 13, Aug, 2024