Open In App

Explain the Process Object in Node.js

Last Updated : 05 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are going to explore about process object of Node.js in detail. The Process object in node.js is a global object that can be used in any module without requiring it in the environment. This object is useful in the perspective of getting the information related to the node.js environment and sets of the runtime of a program.

 Listed below are the events of the process:

  • beforeExit
  • exit
  • config
  • argv

We will describe each of the events below:

1. beforeExit Event: This event is fired when all the task of the program is done and the program is about to exit but it will always execute before the exit event.

Note: You will more clarification between exit Event and beforeExit Event in the next example.

Let’s understand with the help of an implementation as explained below:

Javascript




process.on('beforeExit', function (code) {
    console.log("Executed After finishing "
        + "all the tasks with code: ", code);
});
 
function add(arr) {
    let ans = 0
    for (let ele of arr) {
        ans += ele
    }
    return ans
}
 
const store = add([1, 2, 3, 4, 5]);
console.log("Addition :", store);


Output:

Addition : 15
Executed After finishing all the tasks with code:  0

2. exit Event: This event is fired or executed when the program has finished all the tasks and is just about to exit from the program but it always gets fired after the beforeExit Event.

Let’s understand with the help of an implementation as explained below:

Javascript




process.on('exit', function (code) {
    console.log("Executed After the "
        + "beforeExit Event: ", code);
});
 
process.on('beforeExit', function (code) {
    console.log("Executed After finishing "
        + "all the tasks with code: ", code);
});
 
function add(arr) {
    let ans = 0
    for (let ele of arr) {
        ans += ele
    }
    return ans
}
 
const store = add([1, 2, 3, 4, 5]);
console.log("Addition :", store);


Output:

Addition : 15
Executed After finishing all the tasks with code:  0
Executed After the beforeExit Event:  0

Explanation: As you see that the exit event has been executed after the beforeExit Event. So, now you would have got a clear difference between the exit Event and beforeExit Event.

3. config Event: This event gives the object of the current node.js executable file and gives brief information about the currently running file.

Let’s understand with the help of an implementation as explained below:

Javascript




function add(arr) {
    let ans = 0
    for (let ele of arr) {
        ans += ele
    }
    return ans
}
 
const store = add([1, 2, 3, 4, 5]);
console.log("Addition :", store);
 
console.log(process.config);


Output:

Config Event Output Image

 

4. argv Event: The process.argv property gives an array containing the command-line arguments passed. The first argument always gives the executable path and the second argument will give us the path of the executable javascript file and the remaining will be an additional argument for the command line argument.

Let’s understand with the help of an implementation as explained below:

Javascript




process.argv.forEach((val, index) => {
    console.log(index + ":" + val);
});


Output:

argv Event Output Image

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads