Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Colorize Terminal Output Nodejs
Ayekple Clemence
Ayekple Clemence

Posted on • Originally published atanansewaa.com

     

Colorize Terminal Output Nodejs

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)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

I'm a Fullstack Developer and Blogger. I love trying new things and sharing them
  • Location
    Ghana
  • Work
    Fullstack Developer at University of Cape Coast
  • Joined

More fromAyekple Clemence

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp