Node.js Process Complete Reference

A process object is a global object that gives information about and controls the node.js process. As it is global, it can be used in the project without importing it from any module. 

Table of Content

  • Understanding Node.js Processes
  • Events and Event-driven Architecture
  • Execution Models and Asynchronous Programming

Understanding Node.js Processes

Single-threaded Event Loop

Node.js operates on a single-threaded event loop architecture, allowing it to handle concurrent operations efficiently without the need for multi-threading.

Process Object

The process object in Node.js provides information and control over the Node.js process. It includes properties like process.pid (process ID), process.argv (arguments passed to the process), and process.env (environment variables).

Child Processes

Node.js allows for the creation of child processes to execute external commands or run additional Node.js scripts asynchronously. This enables parallel processing and improves performance.

Events and Event-driven Architecture

EventEmitter Class

The EventEmitter class in Node.js facilitates event-driven programming by allowing objects to emit named events asynchronously. Developers can create custom events and define listeners to handle them.

Core Events

Node.js core modules, such as HTTP, File System, and EventEmitter, utilize event-driven architecture extensively. Understanding core events and their event emitters is essential for building Node.js applications.

Execution Models and Asynchronous Programming

Non-blocking I/O

Node.js employs non-blocking, asynchronous I/O operations, enabling it to handle multiple requests concurrently without waiting for previous operations to complete. This improves application responsiveness and scalability.

Callbacks

Callbacks are a common pattern in Node.js for handling asynchronous operations. Functions accept callbacks as arguments, which are executed once the operation completes. However, callback-based code can lead to callback hell and make code hard to maintain.

Promises

Promises provide a cleaner alternative to callbacks for managing asynchronous operations in Node.js. They represent the eventual completion or failure of an asynchronous operation and allow chaining of multiple asynchronous tasks.

Async/Await

Introduced in ES2017, async/await syntax simplifies asynchronous code in Node.js by allowing developers to write asynchronous code in a synchronous style. It enhances code readability and reduces the complexity associated with nested callbacks or promise chains.

Example:

Javascript
// Node.js program to demonstrate the
// process.versions property

// Include process module
const process = require('process');

// Printing process.versions property value
// and variable count
let no_versions = 0;

// Calling process.versions property
const versions = process.versions;

// Iterating through all returned data
for (let key in versions) {
    // Printing key and its versions
    console.log(key + ":\t\t\t" + versions[key]);
    no_versions++;
}

// Printing count value
console.log("Total no of values available = " + no_versions);

Output:

http_parser:            2.8.0
node: 10.16.0
v8: 6.8.275.32-node.52
uv: 1.28.0
zlib: 1.2.11
brotli: 1.0.7
ares: 1.15.0
modules: 64
nghttp2: 1.34.0
napi: 4
openssl: 1.1.1b
icu: 64.2
unicode: 12.1
cldr: 35.1
tz: 2019a
Total no of values available = 15

The Complete List of Processes is listed below:

Node.js Process (Method)

Description
Node.js process.chdir() Method This is used to change the current working directory.
Node.js process.cpuUsage() MethodThis is used to get the user, and system CPU time usage of the current process.
Node.js process.cwd() MethodThis is used to get the current working directory of the node.js process. 
Node.js process.getegid() MethodThis is used to get the numerical effective group identity of the Node.js process.
Node.js process.geteuid() MethodThis is used to get the numerical effective user identity of the Node.js process.
Node.js process.getgid() MethodThis is used to get the numerical group identity of the Node.js process.
Node.js process.getgroups() MethodThis is used to get the supplementary group IDs.
Node.js process.getuid() MethodThis is used to get the numerical user identity of the Node.js process.
Node.js process.hasUncaughtExceptionCaptureCallback() MethodThis is used to get whether a callback has been set using the process
Node.js process.setegid() MethodThis is used to set the numerical effective group identity of the Node.js process.
Node.js process.seteuid() MethodThis is used to set the effective user identity of the Node.js process.
Node.js process.setgid() MethodThis is used to set the group identity of the Node.js process.
Node.js process.setgroups() MethodThis is used to set the supplementary group IDs.
Node.js process.setuid() MethodThis is used to set the user identity of the Node.js process.
Node.js process.setUncaughtExceptionCaptureCallback() MethodThis is used to set a callback function that will be called when an Uncaught Exception occurs.
Node.js process.uptime() MethodThis is used to get the number of seconds the Node.js process is running.

Node.js Process (Property)

Description

Node.js process.arch PropertyThis is used to get the CPU architecture of the computer for which the current node.js is compiled.
Node.js process.argv PropertyThis is used to get the arguments passed to the node.js process when run in the command line.
Node.js process.argv0 PropertyThis is used to get the read-only copy of the original value of argv[0] passed to the node.js 
Node.js process.config PropertyThis is used to get the JavaScript representation of the configure options that are used to compile the current node.js code.
Node.js process.debugPort PropertyThis is used to get the debugging port used by the debugger if enabled.
Node.js process.env PropertyThis is used to get the user environment. 
Node.js process.execArgv Property This is used to get the node.js specific command-line options passed to the node.js process during launch.
Node.js process.execPath Property This is used to get the absolute pathname of the node.js executable which started the node.js process.
Node.js process.mainModule PropertyThis is used to get the main module. 
Node.js process.pid PropertyThis is used to get the PID of the process.
Node.js process.platform PropertyThis is used to get the Operating System platform information.
Node.js process.ppid Property This is used to get the PID of the current parent process.
Node.js process.release PropertyThis is used to get the metadata related to the current release of node.js.
Node.js process.title PropertyThis is used to get and set the title of the process.
Node.js process.version Property This is used to check the node.js version.
Node.js process.versions PropertyThis is used to get the versions of node.js modules and their dependencies.