
Most developers today work with the terminal. It can be fun and extremely helpful to colorize the terminal output. I have seen a couple of articles using ANSI escape codes to colorize the console output.
The module colors.js and chalk are available on npm. These packages provide extremely easy to use wrappers that make colorizing console output fun.
Let's get started with colorizing our console outputs with node packages.
But first, make sure you are in your project directory.
colors.js
Getting started with colors.js.
Installing colors.js
Let's add thecolors.js to your project:
# via yarnyarn add colors# via npmnpm install colors
Now, within your script, require colors.js or use theES6 import:
constcolors=require#orimportcolorsfrom'colors'
Colorize Terminal Output with colors.js
Withcolors.js you are able to add text colors, brighten text colors, give background colors and brighten background colors.
Colorizing terminal output can be done in two ways withcolors.js.
Super Nifty Way
constcolors=require('colors')console.log('colorizing terminal with colors.js can be fun'.red)console.log('colors make the terminal lively.'.green)
Slightly Less Nifty Way
constcolors=require('colors/safe')console.log(colors.red('colorizing terminal with colors.js can be fun'))console.log(colors.green('colors make the terminal lively.'))
Configuring Custom Theme
It is possible to configure your own custom theme with colors.js standard API.
Using Standard API
constcolors=require('colors')colors.setTheme({info:'blue',warn:'yellow',success:'green',debug:'cyan',error:'red'})console.log('ERROR: Something is wrong with your code'.error)
Using string Safe API
constcolors=require('colors/safe')colors.setTheme({info:'blue',warn:'yellow',success:'green',debug:'cyan',error:'red'})console.log(colors.error('ERROR: Something is wrong with your code'))
You can do more with colors.js custom themes. Check out theirGitHub repository for more.
chalk
chalk package makes it easier to apply ANSI colors and styles to the terminal output.
Installing chalk
You can add chalk to your project usingyarn ornpm:
# via yarnyarn add chalk# via npmnpm install chalk --save
Colorizing Terminal with chalk
And within your script, require chalk with the code below:
constchalk=require('chalk')
chalk package gives you the power to change text color, background color, text styles and more.
Now, let's try our hands on the wonderful features ofchalk.
Changing Text Color with chalk
console.log(chalk.green('colorizing terminal with chalk can be fun'))
Changing Background Color with chalk
console.log(chalk.bgBlackBright('Text Background'))
However, it is possible to add background color and text color in a console output
console.log(chalk.bgCyan.red('Text with background'))
Styling with chalk
Styling works just like colorizing output, we can add it to the chain:
console.log(chalk.bgWhite.black.underline('Styling with chalk'))
We can perform more advanced colorizing with chalk to support other colors that are not part of their 16 color pairs.
console.log(chalk.hex('#421EDA').bgHex('#2534AD')('Advanced Colorizing'))
Conclusion
My goal is to introduce to colors.js and chalk npm packages. You can do more with these packages than I have in this post. Just check out thecolors.js andchalk repository for some more advanced steps.
You can also check out my post onStyling Console Messages.
Top comments(0)
For further actions, you may consider blocking this person and/orreporting abuse