Console
Learn about the console module in Node.js.
We'll cover the following
Debugging made easy
In this lesson, we shall look at a few different functionalities that this module provides.
The console module that Node.js provides is similar to the JavaScript console that most browsers provide. In case you had not noticed, we were not importing the console
module before using console.log
. The reason we were not importing anything is that there is a global console
instance that is configured to write to process.stdout
and process.stderr
. Let’s look at some of the options that the global console
provides.
console.log('Hello World');console.warn('This is a warning!');console.error('This is an error');console.error(new Error('This is a different error'));
You can notice that both the console.warn
and console.error
have their output in a red box. Educative’s code widget uses that to represent an error. Furthermore, we have two console.errors
: one on line 5 and another on line 7. We pass the second console.error
, an Error
object with a string instead of just a string, which is why we get a trace
as well.
Error objects capture a stack trace, which points to where the Error was instantiated in the code.
These console
methods can help track down bugs and problems with code.
console.trace()
The console.trace
is used to print a stack trace. As we’ve seen above, a stack trace is a very useful debugging tool. It shows us the call stack when an exception is thrown or when we call console.trace
. This allows us to trace back to where the problem originated from within the code. Let’s see it in action.
var smallFunction = function (){console.trace('Let us look at the trace');}var bigFunction = function () {smallFunction()}bigFunction()
Looking at the example above we can see that:
- The function where the
console.trace
function was called is namedsmallFunction
. - The function is located in a file at
/usrcode/index.js
. - The function was called on column 11 of line 2.
We see a similar output when an error occurs. This trace output can help us a lot, especially as our programs grow bigger and more complex.
More methods
As Node.js improves over time, new features get added to it. While it may not be possible to keep track of them all, we will discuss a few more methods for console
below.
console.time('For loop time');for (let i = 0; i < 100; i++) {// processing}console.timeEnd('For loop time');console.table([{ "Fruit": "Apple", "Quantity": 5 }, { "Fruit": "Mango", "Quantity": 7 }]);
- The
console.time
in line 1 starts a timer with a name that we pass to it as a string. - The
console.timeEnd
in line 5 stops the timer with the name we pass to it. It prints the time elapsed since the timer was started. - The
console.table
outputs tabular data in a table-like format on the console.
The Console class
Previously, we were using the global console
for printing to the console. What if we wanted to output to a file? For that, we use the Console
class. This allows us to make a simple
const fs = require('fs'); const { Console } = require('console'); const output = fs.createWriteStream('./stdout.log'); const errorOutput = fs.createWriteStream('./stderr.log'); const logger = new Console({ stdout: output, stderr: errorOutput }); const number = 5; logger.log('number:', number); // In stdout.log: number 5 const code = 9 logger.error('error code:', code);
Get hands-on with 1300+ tech skills courses.