Node.js Process
Node.js Process
In Node.js, the process object is a powerful global utility that helps developers access essential information about the running application. It provides details such as process ID, architecture, platform, Node.js version, uptime, memory usage, and more. Beyond information, the process object can also perform actions like killing a process, setting user IDs, and handling signals.
Introduction to Applied AI:–Click Here
Since process
is a global object and an instance of EventEmitter, it can be accessed from anywhere in a Node.js application.
Data Science Tutorial:-Click Here
Node.js Process Properties
Below are some of the commonly used properties of the process
object:
Property | Description |
---|---|
arch | Returns the processor architecture: 'arm' , 'ia32' , or 'x64' . |
argv | Returns command-line arguments as an array. |
env | Provides user environment information. |
pid | Returns the process ID of the running process. |
platform | Returns the platform: 'darwin' , 'freebsd' , 'linux' , 'sunos' , or 'win32' . |
release | Returns metadata about the current Node.js release. |
version | Returns the Node.js version. |
versions | Returns Node.js version and its dependencies. |
Example 1: Basic Process Properties
Download New Real Time Projects :–Click here
File: process_example1.js
console.log(`Process Architecture: ${process.arch}`);
console.log(`Process PID: ${process.pid}`);
console.log(`Process Platform: ${process.platform}`);
console.log(`Process Version: ${process.version}`);
Run in terminal:
node process_example1.js
Example 2: Command-Line Arguments
Machine Learning Tutorial:–Click Here
File: process_example2.js
process.argv.forEach((value, index) => {
console.log(`${index}: ${value}`);
});
Run in terminal:
node process_example2.js
Here, Node.js itself is the first argument, the file name is the second, and any extra arguments are listed after that.
Complete Advance AI topics:-Â CLICK HERE
Node.js Process Functions
Apart from properties, process
also provides useful functions:
Function | Description |
---|---|
cwd() | Returns the current working directory path. |
hrtime() | Returns high-resolution real time as [seconds, nanoseconds] . |
memoryUsage() | Provides an object with memory usage details. |
process.kill(pid[, signal]) | Kills a given process by its PID. |
uptime() | Returns the uptime of the Node.js process in seconds. |
Example 3: Current Directory & Uptime
Deep Learning Tutorial:– Click Here
File: process_example3.js
console.log(`Current directory: ${process.cwd()}`);
console.log(`Uptime: ${process.uptime()} seconds`);
Run in terminal:
node process_example3.js
Complete Python Course with Advance topics:-Click Here
SQL Tutorial :–Click Here
node js process model
node js process model diagram
node js process model in w3schools
node js process exit gracefully
node process exit
process.on uncaughtexception
process.exit(1) in node js
cannot find module ‘node:process’
node js process example
node js process w3schools
Post Comment