Node.js Process

Last Updated : 21 Aug 2026

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.

What is the Process Object in Node.js?

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:

  • Access the process and system information
  • Read command-line arguments
  • Work with environment variables
  • Monitor memory and CPU usage
  • Handle process events
  • Control the application execution

How to Access the Process Object in Node.js

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.

Example

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.

Note: Please note that the process ID, Node.js version and platform shown in the output may vary depending on your system.

Process Properties in Node.js

The process object has many properties that permit developers to access information about the currently running Node.js process and its execution environment.

PropertyDescription
process.pidIt returns the process ID of the current Node.js process.
process.ppidIt returns the parent process ID.
process.platformIt returns the operating system platform.
process.archIt returns the CPU architecture.
process.versionIt returns the Node.js version.
process.versionsIt returns version information of Node.js dependencies.
process.argvIt returns command-line arguments passed to the application.
process.argv0It returns the original executable name.
process.execPathIt returns the absolute path of the Node.js executable.
process.execArgvIt returns Node.js-specific command-line options.
process.envIt returns environment variables as an object.
process.titleIt gets or sets the process title.

Process Methods in Node.js

The process object has various methods that assist developers manage, monitor and control the execution of a Node.js application.

MethodDescription
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.

Common Process Events in Node.js

Node.js provides several process events that allow developers to respond to important lifecycle events, errors, warnings and system signals.

EventDescription
exitIt occurs when the process is about to exit.
beforeExitIt occurs before the Node.js event loop ends.
uncaughtExceptionIt occurs when an exception is not handled.
unhandledRejectionIt occurs when a Promise rejection is not handled.
warningIt occurs when Node.js emits a warning.
SIGINTIt is triggered when the user interrupts the process (for example, pressing Ctrl + C).
SIGTERMIt is triggered when the process receives a termination signal.
messageIt is triggered when a message is received through an IPC (Inter-Process Communication) channel.

Examples of the Process Object

The following examples demonstrate some common uses of the Node.js process object:

Example 1: Reading Command-Line Arguments

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.

Example 2: Accessing Environment Variables

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.

Note: The values of environment variables depend on the operating system and user account.

Example 3: Handling Process Events

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.

Example 4: Checking Memory Usage

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.

Example 5: Getting the Current Working Directory

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.