![]() |
VOOZH | about |
The console module provides a simple debugging console that is provided by web browsers which export two specific components:
The new Console() (Added in v8.0.0) method is an inbuilt application programming interface of the 'console' module which creates a new Console with single or multiple writable stream instances in which stdout is a writable stream and stderr is used for warning or error output. If stderr is not provided, stdout is used for stderr. It is a console whose output is sent to process.stdout and process.stderr.
Note: The global console methods are neither consistently synchronous nor consistently asynchronous.
Syntax:
new Console(options);
Arguments:
const options = {
stdout: writableStream,
stderr: writableStream,
ignoreErrors: true,
colorMode:true
};
In order to use this method, we need to create a console using (new Console()) method, and we need to import the ‘console’ and 'fs' module.
const console = require('console');
const fs = require('fs');
Parameters: This function accepts an object/list of parameters as mentioned above and described below:
options <Object>: It may have the following elements in the options object.
Return Value: Its output is sent to process.stdout and process.stderr files which are created by fs module through <stream.Writable>.
The Below examples illustrate the use of new Console(options) method in Node.js.
Example 1: Filename: index.js
Run index.js file using the following command:
node index.js
Output:
Note: The above node.js example will create log files (out & err) in the same folder where index.js file exists.
>> Successfully created and logged via console...
Example 2: Filename: index.js
Run index.js file using the following command:
node index.js
Output:
Note: The above node.js example will create log files (output & error) in the same folder where index.js file exists.
Family Stream Created: {
Head: 'Miss Sanno',
headDesignation: 'Teacher',
Member1: 'Miss Sanchi',
member1Designation: 'Kid',
Member2: 'Master Amit',
member2Designation: 'Student'
}
Count Stream Created: 397.5
Reference: https://nodejs.org/api/console.html#console_new_console_options