Console Module

Similar to the browser environment of JavaScript node.js provides a console module which provides simple logging and debugging possibilities.

The most important methods provided by the console module are console.log, console.error and console.time. But there are several others like console.info.

console.log

The parameters will be printed to the standard output (stdout) with a new line.

console.log('Hello World');

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/e0be3db8-3d42-4ac5-9211-158db849aa2c/Untitled.png

console.error

The parameters will be printed to the standard error (stderr) with a new line.

console.error('Oh, sorry, there is an error.');

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/4b09b7f5-a36e-4fe1-b17c-4eb6af0642bc/Untitled.png

console.time, console.timeEnd

console.time starts a timer with an unique lable that can be used to compute the duration of an operation. When you call console.timeEnd with the same label, the timer stops and it prints the elapsed time in milliseconds to stdout.

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/651a86a1-e4c5-44b0-aec5-3c09cb3a1d41/Untitled.png

Process Module

It is possible to use the process module to write directly into the standard output of the console. Therefore it exists the method process.stdout.write. Unlike console.log this method does not add a new line before your output.

So in the following example the method is called two times, but no new line is added in between their outputs.

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/d15c5ce4-0603-47af-91fd-d916f21bbba5/Untitled.png

Formatting

One can use terminal (control) codes to issue specific commands like switching colors or positioning the cursor.