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
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:
// 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() Method | This is used to get the user, and system CPU time usage of the current process. |
Node.js process.cwd() Method | This is used to get the current working directory of the node.js process. |
Node.js process.getegid() Method | This is used to get the numerical effective group identity of the Node.js process. |
Node.js process.geteuid() Method | This is used to get the numerical effective user identity of the Node.js process. |
Node.js process.getgid() Method | This is used to get the numerical group identity of the Node.js process. |
Node.js process.getgroups() Method | This is used to get the supplementary group IDs. |
Node.js process.getuid() Method | This is used to get the numerical user identity of the Node.js process. |
Node.js process.hasUncaughtExceptionCaptureCallback() Method | This is used to get whether a callback has been set using the process |
Node.js process.setegid() Method | This is used to set the numerical effective group identity of the Node.js process. |
Node.js process.seteuid() Method | This is used to set the effective user identity of the Node.js process. |
Node.js process.setgid() Method | This is used to set the group identity of the Node.js process. |
Node.js process.setgroups() Method | This is used to set the supplementary group IDs. |
Node.js process.setuid() Method | This is used to set the user identity of the Node.js process. |
Node.js process.setUncaughtExceptionCaptureCallback() Method | This is used to set a callback function that will be called when an Uncaught Exception occurs. |
Node.js process.uptime() Method | This is used to get the number of seconds the Node.js process is running. |
Node.js Process (Property) | Description |
---|---|
Node.js process.arch Property | This is used to get the CPU architecture of the computer for which the current node.js is compiled. |
Node.js process.argv Property | This is used to get the arguments passed to the node.js process when run in the command line. |
Node.js process.argv0 Property | This is used to get the read-only copy of the original value of argv[0] passed to the node.js |
Node.js process.config Property | This 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 Property | This is used to get the debugging port used by the debugger if enabled. |
Node.js process.env Property | This 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 Property | This is used to get the main module. |
Node.js process.pid Property | This is used to get the PID of the process. |
Node.js process.platform Property | This 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 Property | This is used to get the metadata related to the current release of node.js. |
Node.js process.title Property | This 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 Property | This is used to get the versions of node.js modules and their dependencies. |