What is a process in Node.js?

Processes in Operating Systems are like containers in which our applications are run. When a process exits or crashes, the OS cleans up the resources allocated to it, such as memory, sockets, threads, etc.

The node itself is a host environment for running javascript. The process module in Node.js allows us to get information related to the node and control its behavior. This module is globally available, and we can use it to get information such as process ID, version, release, CPU usage, and uptime or perform actions such as killing processes, setting uid, setting groups, unmasking, etc.

Code example 1

Let's look at the code below:

// Including the module into out project
var process = require('process');
// Viewing the current working directory
console.log('Working directory --> ' + process.cwd());
// Viewing the version of process we are using
console.log('Process version --> ' + process.version);
// Viewing the type of OS we are using
console.log('Current OS --> ' + process.platform);
// Viewing the arguments passed
console.log('Arguments --> ', process.argv);
// Viewing the Feature Object
console.log('Feature Property: \n', process.features);

Code explanation

  • Line 2: We include the process module in our code.

  • Line 5: We print the current working directory using process.

  • Line 8: We print the version of process that we are using.

  • Line 11: We print the type of OS we are working on.

  • Line 14: We print the arguments passed when the current application was run.

  • Line 17: We view the Feature Object in process.

Code example 2

The process module also has the node version and its build dependencies. They can be accessed as follows:

// Including the 'process' module into out project
var process = require('process');
// Declaring a counter variable
var no_versions = 0;
// Calling process.versions property
var versions = process.versions;
// Iterating through returned data
for (var key in versions) {
// Printing key and its versions
console.log(key + "\t".repeat(2-Math.floor((key.length-8)/8)) + versions[key]);
no_versions++;
}
// Printing count value
console.log("\nTotal no of values available = " + no_versions);

Code explanation

  • Line 2: We include the process module into our project.

  • Line 8: We call the versions property in process.

  • Line 14: We print keys and their versions that were retrieved from the process module in line 8.

  • Line 20: We print the number of packages in the build dependencies for our current version of Node.

Code example 3

The process module has its own pre-defined events which can be used, for example, to be notified when a program exits:

// Including the 'process' module into out project
var process = require('process');
// Define a funtion that will be triggered when an application exits
process.on('exit', code => {
setTimeout(() => {
console.log('Will not get displayed');
}, 0);
console.log('Exited with status code:', code);
});
// Run an application
console.log('Execution Completed');

Code explanation

  • Line 2: We include the process module.

  • Lines 5-11: We write a function that is triggered in the event an application exits. We have run a line of code console.log(‘Will not get displayed’) that is timed to run as soon as the app exits. We don't see any output from the setTimeout function as the application has already exited.

  • Line 14: We run the application.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved