Skip to main content

Command Palette

Search for a command to run...

Output to the command Line using Node.js

Published
2 min read
Output to the command Line using Node.js

Node.js provides a console module which provides tons of very useful ways to interact with the command line. It is basically the same as the console you find in the browser.

The most basic and most used method is console.log(), which prints the string you pass to it to the console.

We can also format pretty phrases by passing variables and a format specifier.

  • %s format a variable as a string
  • %d format a variable as a number
  • %i format a variable as its integer part only
  • %o format a variable as an object

console.clear() clears the console (the behavior might depend on the console used)

console.count() counts the number of times a string is printed, and prints the count next to it:

The console.countReset()method resets counter used with console.count().

Print the stack trace

There might be cases where it’s useful to print the call stack trace of a function, maybe to answer the question how did you reach that part of the code?

You can do so using console.trace():

Calculate the time spent

You can easily calculate how much time a function takes to run, using time() and timeEnd()

The simplest way to go about coloring the console output is by using a library. Chalk is such a library, and in addition to coloring it also helps with other styling facilities, like making text bold, italic or underlined.

You install it with npm install chalk

Create a progress bar

Progress is an awesome package to create a progress bar in the console. Install it using npm install progress

This snippet creates a 10-step progress bar, and every 100ms one step is completed.

More from this blog

C

Charles Kasasira - Fullstack developer

21 posts

Software engineer aiming to design and develop experiences that make people's lives simple.