Output to the command line using Node.js
Basic output using the console module
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
object 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.
If you pass an object, it will render it as a string.
You can pass multiple variables to console.log
, for example:
const const x: "x"
x = 'x';
const const y: "y"
y = 'y';
var console: Console
console.Console.log(...data: any[]): void
[MDN Reference](https://developer.mozilla.org/docs/Web/API/console/log_static)log(const x: "x"
x, const y: "y"
y);
and Node.js will print both.
We can also format pretty phrases by passing variables and a format specifier.
For example:
var console: Console
console.Console.log(...data: any[]): void
[MDN Reference](https://developer.mozilla.org/docs/Web/API/console/log_static)log('My %s has %d ears', 'cat', 2);
%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
Example:
var console: Console
console.Console.log(...data: any[]): void
[MDN Reference](https://developer.mozilla.org/docs/Web/API/console/log_static)log('%o', var Number: NumberConstructor
An object that represents a number of any kind. All JavaScript numbers are 64-bit floating-point numbers.Number);
Clear the console
console.clear()
clears the console (the behavior might depend on the console used)
Counting elements
console.count()
is a handy method.
Take this code:
const const x: 1
x = 1;
const const y: 2
y = 2;
const const z: 3
z = 3;
var console: Console
console.Console.count(label?: string): void
[MDN Reference](https://developer.mozilla.org/docs/Web/API/console/count_static)count(
'The value of x is ' + const x: 1
x + ' and has been checked .. how many times?'
);
var console: Console
console.Console.count(label?: string): void
[MDN Reference](https://developer.mozilla.org/docs/Web/API/console/count_static)count(
'The value of x is ' + const x: 1
x + ' and has been checked .. how many times?'
);
var console: Console
console.Console.count(label?: string): void
[MDN Reference](https://developer.mozilla.org/docs/Web/API/console/count_static)count(
'The value of y is ' + const y: 2
y + ' and has been checked .. how many times?'
);
What happens is that console.count()
will count the number of times a string is printed, and print the count next to it:
You can just count apples and oranges:
const const oranges: string[]
oranges = ['orange', 'orange'];
const const apples: string[]
apples = ['just one apple'];
const oranges: string[]
oranges.Array<string>.forEach(callbackfn: (value: string, index: number, array: string[]) => void, thisArg?: any): void
Performs the specified action for each element in an array.forEach(fruit: string
fruit => {
var console: Console
console.Console.count(label?: string): void
[MDN Reference](https://developer.mozilla.org/docs/Web/API/console/count_static)count(fruit: string
fruit);
});
const apples: string[]
apples.Array<string>.forEach(callbackfn: (value: string, index: number, array: string[]) => void, thisArg?: any): void
Performs the specified action for each element in an array.forEach(fruit: string
fruit => {
var console: Console
console.Console.count(label?: string): void
[MDN Reference](https://developer.mozilla.org/docs/Web/API/console/count_static)count(fruit: string
fruit);
});
Reset counting
The console.countReset() method resets counter used with console.count().
We will use the apples and orange example to demonstrate this.
const const oranges: string[]
oranges = ['orange', 'orange'];
const const apples: string[]
apples = ['just one apple'];
const oranges: string[]
oranges.Array<string>.forEach(callbackfn: (value: string, index: number, array: string[]) => void, thisArg?: any): void
Performs the specified action for each element in an array.forEach(fruit: string
fruit => {
var console: Console
console.Console.count(label?: string): void
[MDN Reference](https://developer.mozilla.org/docs/Web/API/console/count_static)count(fruit: string
fruit);
});
const apples: string[]
apples.Array<string>.forEach(callbackfn: (value: string, index: number, array: string[]) => void, thisArg?: any): void
Performs the specified action for each element in an array.forEach(fruit: string
fruit => {
var console: Console
console.Console.count(label?: string): void
[MDN Reference](https://developer.mozilla.org/docs/Web/API/console/count_static)count(fruit: string
fruit);
});
var console: Console
console.Console.countReset(label?: string): void
[MDN Reference](https://developer.mozilla.org/docs/Web/API/console/countReset_static)countReset('orange');
const oranges: string[]
oranges.Array<string>.forEach(callbackfn: (value: string, index: number, array: string[]) => void, thisArg?: any): void
Performs the specified action for each element in an array.forEach(fruit: string
fruit => {
var console: Console
console.Console.count(label?: string): void
[MDN Reference](https://developer.mozilla.org/docs/Web/API/console/count_static)count(fruit: string
fruit);
});
Notice how the call to console.countReset('orange')
resets the value counter to zero.
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()
:
const const function2: () => void
function2 = () => var console: Console
console.Console.trace(...data: any[]): void
[MDN Reference](https://developer.mozilla.org/docs/Web/API/console/trace_static)trace();
const const function1: () => void
function1 = () => const function2: () => void
function2();
const function1: () => void
function1();
This will print the stack trace. This is what's printed if we try this in the Node.js REPL:
Trace
at function2 (repl:1:33)
at function1 (repl:1:25)
at repl:1:1
at ContextifyScript.Script.runInThisContext (vm.js:44:33)
at REPLServer.defaultEval (repl.js:239:29)
at bound (domain.js:301:14)
at REPLServer.runBound [as eval] (domain.js:314:12)
at REPLServer.onLine (repl.js:440:10)
at emitOne (events.js:120:20)
at REPLServer.emit (events.js:210:7)
Calculate the time spent
You can easily calculate how much time a function takes to run, using time()
and timeEnd()
const const doSomething: () => void
doSomething = () => var console: Console
console.Console.log(...data: any[]): void
[MDN Reference](https://developer.mozilla.org/docs/Web/API/console/log_static)log('test');
const const measureDoingSomething: () => void
measureDoingSomething = () => {
var console: Console
console.Console.time(label?: string): void
[MDN Reference](https://developer.mozilla.org/docs/Web/API/console/time_static)time('doSomething()');
// do something, and measure the time it takes
const doSomething: () => void
doSomething();
var console: Console
console.Console.timeEnd(label?: string): void
[MDN Reference](https://developer.mozilla.org/docs/Web/API/console/timeEnd_static)timeEnd('doSomething()');
};
const measureDoingSomething: () => void
measureDoingSomething();
stdout and stderr
As we saw console.log is great for printing messages in the Console. This is what's called the standard output, or stdout
.
console.error
prints to the stderr
stream.
It will appear in the console, but can be handled separately from regular output.
Color the output
NOTE This part of the resource is designed with version 22.11 which notes
styleText
as ‘Active development’.
In many cases, you will be tempted to paste certain text to get a nice output at the terminal.
There is a styleText
function provided by the node:util
module. Let's discover how to use it.
First of all, you need to import the styleText
function from the node:util
module:
import { import styleText
styleText } from 'node:util';
Then, you can use it to style your text:
var console: Console
console.Console.log(...data: any[]): void
[MDN Reference](https://developer.mozilla.org/docs/Web/API/console/log_static)log(
styleText(['red'], 'This is red text ') +
styleText(['green', 'bold'], 'and this is green bold text ') +
'this is normal text'
);
The first argument is an array of styles, and the second argument is the text you want to style. We invite you to read the docs