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.
Let's look at the code below:
// Including the module into out projectvar process = require('process');// Viewing the current working directoryconsole.log('Working directory --> ' + process.cwd());// Viewing the version of process we are usingconsole.log('Process version --> ' + process.version);// Viewing the type of OS we are usingconsole.log('Current OS --> ' + process.platform);// Viewing the arguments passedconsole.log('Arguments --> ', process.argv);// Viewing the Feature Objectconsole.log('Feature Property: \n', process.features);
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.
The process module also has the node version and its build dependencies. They can be accessed as follows:
// Including the 'process' module into out projectvar process = require('process');// Declaring a counter variablevar no_versions = 0;// Calling process.versions propertyvar versions = process.versions;// Iterating through returned datafor (var key in versions) {// Printing key and its versionsconsole.log(key + "\t".repeat(2-Math.floor((key.length-8)/8)) + versions[key]);no_versions++;}// Printing count valueconsole.log("\nTotal no of values available = " + no_versions);
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.
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 projectvar process = require('process');// Define a funtion that will be triggered when an application exitsprocess.on('exit', code => {setTimeout(() => {console.log('Will not get displayed');}, 0);console.log('Exited with status code:', code);});// Run an applicationconsole.log('Execution Completed');
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