Colorful Node.js Message Logging with Chalk

By  on  

As you work more and more with Node.js, you start to see the value of good logging, especially to the console.  The problem you run into, however, is that constantly adding logged messages means that the most important messages can get lost in the shuffle.  Info messages should look one way and app-killing errors should look another.  The Node.js module to help us accomplish custom formatting of messages?  Chalk!

Chalk has a very easy to follow, simple to use API.  Here are a few code examples:

const chalk = require('chalk');

// style a string
chalk.blue('Hello world!');

// combine styled and normal strings
chalk.blue('Hello') + 'World' + chalk.red('!');

// compose multiple styles using the chainable API
chalk.blue.bgRed.bold('Hello world!');

// pass in multiple arguments
chalk.blue('Hello', 'World!', 'Foo', 'bar', 'biz', 'baz');

// nest styles
chalk.red('Hello', chalk.underline.bgBlue('world') + '!');

You can chain methods like bold onto color names, and visa versa.  You can also append Chalk'd strings or add them as separate arguments.  Chalk is very flexible without modifying the String prototype which is impressive.

Apparently over 5,000 projects use Chalk and I can see why!  Big problems should come with big colors and lessor debugging information should be less prominent.  Happy coding!

Recent Features

  • By
    Facebook Open Graph META Tags

    It's no secret that Facebook has become a major traffic driver for all types of websites.  Nowadays even large corporations steer consumers toward their Facebook pages instead of the corporate websites directly.  And of course there are Facebook "Like" and "Recommend" widgets on every website.  One...

  • By
    Create a CSS Flipping Animation

    CSS animations are a lot of fun; the beauty of them is that through many simple properties, you can create anything from an elegant fade in to a WTF-Pixar-would-be-proud effect. One CSS effect somewhere in between is the CSS flip effect, whereby there's...

Incredible Demos

  • By
    Fancy Navigation with MooTools JavaScript

    Navigation menus are traditionally boring, right? Most of the time the navigation menu consists of some imagery with a corresponding mouseover image. Where's the originality? I've created a fancy navigation menu that highlights navigation items and creates a chain effect. The XHTML Just some simple...

  • By
    Translate Content with the Google Translate API and JavaScript

    Note:  For this tutorial, I'm using version1 of the Google Translate API.  A newer REST-based version is available. In an ideal world, all websites would have a feature that allowed the user to translate a website into their native language (or even more ideally, translation would be...

Discussion

  1. Oh my. THANK YOU! This is the exact chalk effect I was looking for one of my projects.

  2. Looks like a cool tool – and pretty colour scheme too :D

  3. Randy

    Yeah idk I’d rather not need to pick the color myself for a logged error message. If you use better-console or captains-log you can just use log.error() or log.info() and get coloring which makes sense.

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!