When a Node.js application is executed then a process is initiated in which the application's code is run and its resources are managed. Node.js provides the process object to communicate with this executing process so as to retrieve information about the application as well as the system.
The process object enables developers to read command-line arguments, work with environment variables, keep track of resource usage and handle process events. It is useful for application configuration, performance monitoring and process management.
The process object is a global object in Node.js that provides information related to the current process. As the process object is a global object so it can be accessed directly without the requirement of importing any module.
The process object allows developers to perform the following functions:
As stated earlier that the process is a global object in Node.js so it can be accessed directly without the need to import any module. It can be used to retrieve information regarding the currently running process such as the process ID, Node.js version and operating system platform.
console.log(process.pid); console.log(process.version); console.log(process.platform);
Output:
12345 v24.0.0 win32
Explanation
In this example, process.pid returns the unique process ID of the running Node.js application. The process.version property displays the current version of installed Node.js version whereas process.platform identifies the operating system platform on which the application is running. These properties are commonly used for debugging, logging, and environment-specific application behavior.
The process object has many properties that permit developers to access information about the currently running Node.js process and its execution environment.
| Property | Description |
|---|---|
| process.pid | It returns the process ID of the current Node.js process. |
| process.ppid | It returns the parent process ID. |
| process.platform | It returns the operating system platform. |
| process.arch | It returns the CPU architecture. |
| process.version | It returns the Node.js version. |
| process.versions | It returns version information of Node.js dependencies. |
| process.argv | It returns command-line arguments passed to the application. |
| process.argv0 | It returns the original executable name. |
| process.execPath | It returns the absolute path of the Node.js executable. |
| process.execArgv | It returns Node.js-specific command-line options. |
| process.env | It returns environment variables as an object. |
| process.title | It gets or sets the process title. |
The process object has various methods that assist developers manage, monitor and control the execution of a Node.js application.
| Method | Description |
|---|---|
| process.cwd() | It returns the current working directory. |
| process.chdir() | It changes the current working directory. |
| process.exit() | It terminates the current process. |
| process.memoryUsage() | It returns memory usage statistics. |
| process.cpuUsage() | It returns CPU usage information. |
| process.uptime() | It returns the process uptime in seconds. |
| process.hrtime() | It returns high-resolution real-time information. |
| process.kill() | It sends a signal to a process. |
| process.nextTick() | It schedules a callback to run before the next event loop iteration. |
Node.js provides several process events that allow developers to respond to important lifecycle events, errors, warnings and system signals.
| Event | Description |
|---|---|
| exit | It occurs when the process is about to exit. |
| beforeExit | It occurs before the Node.js event loop ends. |
| uncaughtException | It occurs when an exception is not handled. |
| unhandledRejection | It occurs when a Promise rejection is not handled. |
| warning | It occurs when Node.js emits a warning. |
| SIGINT | It is triggered when the user interrupts the process (for example, pressing Ctrl + C). |
| SIGTERM | It is triggered when the process receives a termination signal. |
| message | It is triggered when a message is received through an IPC (Inter-Process Communication) channel. |
The following examples demonstrate some common uses of the Node.js process object:
We are going to learn how to read command-line arguments in Node.js using the process.argv property. Command-line arguments permit users to pass additional information to a Node.js application when running it from the terminal.
Code
console.log("Command-Line Arguments:");
console.log(process.argv);
Run the Program
node app.js JavaScript Node.js
Output:
[ 'C:\\Program Files\\nodejs\\node.exe', 'app.js', 'JavaScript', 'Node.js' ]
Explanation
The process.argv property returns an array containing the command-line arguments which were passed to the Node.js application. The first element represents the path of the Node.js executable, the second element is the script file name and the remaining elements contain the arguments provided by the user.
We will here understand how to access environment variables in Node.js using the process.env property. Environment variables store system-level configuration values that can be used by applications during execution.
Code
console.log( "User:", process.env.USERNAME || process.env.USER ); console.log( "Home Directory:", process.env.HOME || process.env.USERPROFILE );
Output:
User: John Home Directory: C:\Users\John
Explanation
Here the process.env property is used to access environment variables that are present in the operating system. The USERNAME variable returns the current user's name while HOME or USERPROFILE returns the path of the user's home directory depending on the operating system.
We will learn how to handle process events in Node.js using the process.on() method. Process events allow developers to perform specific actions when important events occur during the lifecycle of a Node.js application.
Code
process.on('exit', (code) => {
console.log(`Process exiting with code ${code}`);
});
console.log("Application is running...");
Output:
Application is running... Process exiting with code 0
Explanation
In this example, the exit event is triggered when the Node.js process is about to terminate. The callback function receives the exit code of the process as an argument. Here, the application prints a message while running and then displays the exit code before the process ends.
In this example, we will learn how to check the memory usage of a Node.js application utilizing the process.memoryUsage() method.
Code
console.log(process.memoryUsage());
Output:
{
rss: 25427968,
heapTotal: 5271552,
heapUsed: 3984200,
external: 1321456,
arrayBuffers: 10514
}
Explanation
The process.memoryUsage() method returns an object containing memory usage statistics such as RSS, heap usage, external memory and array buffer memory used by the current process.
We will here comprehend how to get the current working directory of a Node.js application using the process.cwd() method.
Code
console.log(process.cwd());
Output:
C:\Projects\NodeApp
Explanation
The process.cwd() method returns the current working directory from which the Node.js application was started.
We request you to subscribe our newsletter for upcoming updates.