Node.js Process Object
Learn how to use the Node.js process object to inspect runtime information, identify the Node.js version, read command-line arguments, access environment variables, monitor process events, and control how an application exits.
1. What Is the Node.js Process Object?
The process object is a global object provided by
Node.js. It contains information about the currently running
Node.js process and provides methods for interacting with it.
A process is an instance of a running program managed by the operating system. When we execute a JavaScript file using Node.js, the operating system runs a Node.js process to execute that application.
The process object helps us access details such as the runtime version, operating system platform, current working directory, command-line arguments, environment variables, and exit events.
process directly in a Node.js JavaScript
file. We do not need to import it manually.
Common Process Properties and Methods
| Property / Method | Purpose | Example |
|---|---|---|
process.version |
Returns the Node.js version. | process.version |
process.platform |
Identifies the operating system platform. | process.platform |
process.arch |
Identifies the CPU architecture used by the Node.js binary. | process.arch |
process.cwd() |
Returns the current working directory. | process.cwd() |
process.argv |
Contains command-line arguments. | process.argv |
process.env |
Provides access to environment variables. | process.env.NODE_ENV |
process.pid |
Returns the process identifier. | process.pid |
process.exitCode |
Sets the exit status to be used when the process exits. | process.exitCode = 1 |
2. How the Process Object Works
When a Node.js application starts, Node.js creates a process context. The process object exposes information about that running instance to the JavaScript program.
node app.js
process Object
Version, Platform, Arguments, Environment
For example, an application can read its environment configuration or examine command-line arguments before performing a task.
3. Check the Node.js Version
The process.version property returns the version
of Node.js running the application. It is useful when checking
which runtime version is executing a program.
Example
console.log("Node.js Version:", process.version);
Execute the Program
node version.js
Example Output
Node.js Version: v22.x.x
The exact version depends on the Node.js installation on your computer. The output above is only an illustrative example.
4. Check Platform and Architecture
Node.js provides properties that identify the operating system platform and the CPU architecture used by the Node.js executable.
Example
console.log("Platform:", process.platform);
console.log("Architecture:", process.arch);
Execute the Program
node systemInfo.js
Example Output on a Typical 64-bit Windows System
Platform: win32
Architecture: x64
Explanation
process.platformcommonly returnswin32on Windows, even on 64-bit Windows systems.process.archcommonly returnsx64for a 64-bit x86 Node.js executable.- The actual values depend on the operating system and Node.js build.
5. Understand process.cwd()
The process.cwd() method returns the current working
directory of the running process. This is the directory from
which the application was launched, not necessarily the folder
where the JavaScript file is stored.
Example
console.log("Current Working Directory:");
console.log(process.cwd());
Execute the Program
node directory.js
Example Output
Current Working Directory:
C:\Projects\NodeJsPractice
Your output will show the actual directory from which the command was executed.
6. Understand process.pid
Every running operating-system process has a process identifier,
commonly called a PID. The process.pid property
returns the identifier assigned to the current Node.js process.
Example
console.log("Application Name: CIIT Node.js Practice");
console.log("Process ID:", process.pid);
Execute the Program
node processId.js
Example Output
Application Name: CIIT Node.js Practice
Process ID: 12345
The number 12345 is an example only. Your process
identifier will usually be different each time you run the program.
7. Read Command-Line Arguments
The process.argv property contains an array of
command-line arguments passed to the Node.js process.
In a typical invocation, the first two entries identify the
Node.js executable and the JavaScript file. Additional entries
contain the arguments supplied by the user.
Example
const studentName = process.argv[2];
const courseName = process.argv[3];
console.log("Student Name:", studentName);
console.log("Course Name:", courseName);
Execute the Program with Arguments
node student.js Samadhan Node.js
Expected Output
Student Name: Samadhan
Course Name: Node.js
How process.argv Works
| Index | Value in This Example | Meaning |
|---|---|---|
process.argv[0] |
Node.js executable path | Identifies the Node.js executable. |
process.argv[1] |
student.js path |
Identifies the executed script. |
process.argv[2] |
Samadhan |
First user-supplied argument. |
process.argv[3] |
Node.js |
Second user-supplied argument. |
undefined. Real applications should validate
required arguments before using them.
8. Access Environment Variables
Environment variables provide configuration values to an application from outside its source code. They are commonly used to select an application mode or configure runtime behavior.
Node.js exposes environment variables through
process.env. Their values are generally represented
as strings, so numeric values may need conversion before
mathematical operations.
Example: Read an Environment Variable
const environment = process.env.APP_MODE || "development";
console.log("Application Mode:", environment);
Run Without Setting the Variable
node environment.js
Expected Output When APP_MODE Is Not Set
Application Mode: development
Set the Variable on Windows PowerShell
$env:APP_MODE="production"
node environment.js
Expected Output
Application Mode: production
The PowerShell variable is set for the current session. Other shells use different commands to set environment variables.
9. Understand the Process Lifecycle
A Node.js process starts when the application is launched. It executes code and remains active while there is work that keeps the event loop running. When the process exits, Node.js provides an exit event that can be used for limited synchronous cleanup or final reporting.
Example: Listen for the Exit Event
process.on("exit", (code) => {
console.log("Process is exiting.");
console.log("Exit Code:", code);
});
console.log("Application is running.");
Execute the Program
node exitExample.js
Expected Output
Application is running.
Process is exiting.
Exit Code: 0
In this simple program, no timer, server, or other active work keeps the process running, so it exits after the synchronous statements finish.
10. Understand process.exitCode
The process.exitCode property sets the exit status
that Node.js will use when the process exits naturally.
By convention, exit code 0 indicates success,
while a non-zero code usually indicates an error or unsuccessful
execution.
Example: Set an Exit Code
const studentName = "Samadhan";
if (studentName) {
console.log("Student data processed successfully.");
process.exitCode = 0;
} else {
console.error("Student name is missing.");
process.exitCode = 1;
}
Execute the Program
node exitCode.js
Expected Output
Student data processed successfully.
This example sets the exit status based on a simple condition. In automated scripts and CI/CD pipelines, exit codes can help determine whether a command succeeded.
process.exitCode when you want
Node.js to exit naturally. Calling process.exit()
terminates the process immediately and may interrupt pending
asynchronous operations.
11. Practical Example: Student Information
Let us combine multiple process properties and command-line arguments in one practical program.
Create studentInfo.js
const studentName = process.argv[2];
const courseName = process.argv[3];
console.log("Student Information");
console.log("-------------------");
console.log("Student:", studentName || "Not provided");
console.log("Course:", courseName || "Not provided");
console.log("Node.js Version:", process.version);
console.log("Operating Platform:", process.platform);
console.log("Process ID:", process.pid);
Run the Program
node studentInfo.js Pradnya ".NET Full Stack"
Example Output
Student Information
-------------------
Student: Pradnya
Course: .NET Full Stack
Node.js Version: v22.x.x
Operating Platform: win32
Process ID: 12345
The Node.js version, platform, and process ID shown above are illustrative. Your computer will display its actual values.
What This Program Demonstrates
- Reads the student name from
process.argv. - Reads the course name, including a value containing spaces.
- Displays the installed Node.js version.
- Displays the operating-system platform.
- Displays the current process identifier.
12. Common Mistakes with the Process Object
| Mistake | Reason | Solution |
|---|---|---|
| Assuming a fixed Node.js version | Different computers may use different installations. | Check process.version. |
| Using an incorrect argument index | User arguments begin after the executable and script entries. | Check the required process.argv index. |
| Assuming a fixed working directory | The program can be launched from different directories. | Inspect process.cwd(). |
| Expecting environment variables to exist | A variable may not be configured in the current shell. | Validate values from process.env. |
| Calling process.exit() unnecessarily | Immediate termination may interrupt pending work. | Use process.exitCode when appropriate. |
13. CIIT Training Institute - Practice Task
Task: Build a Student Runtime Information Program
Create a file named studentRuntime.js and
complete the following activities:
- Read a student name from command-line arguments.
- Read a course name from command-line arguments.
- Display the Node.js version.
- Display the current working directory.
- Display the operating-system platform.
- Display the process ID.
- Handle missing student or course arguments.
Sample Solution
const studentName = process.argv[2];
const courseName = process.argv[3];
if (!studentName || !courseName) {
console.error(
"Usage: node studentRuntime.js <studentName> <courseName>"
);
process.exitCode = 1;
} else {
console.log("CIIT TRAINING INSTITUTE");
console.log("Student:", studentName);
console.log("Course:", courseName);
console.log("Node.js Version:", process.version);
console.log("Working Directory:", process.cwd());
console.log("Platform:", process.platform);
console.log("Process ID:", process.pid);
}
Run the Program
node studentRuntime.js Samadhan "Node.js"
Expected Output Format
CIIT TRAINING INSTITUTE
Student: Samadhan
Course: Node.js
Node.js Version: [your Node.js version]
Working Directory: [your project directory]
Platform: [your operating system]
Process ID: [current process ID]
The values in square brackets will be replaced by actual runtime information from your computer.
Summary
The Node.js process object provides information and controls for the currently running application. We learned how to inspect the Node.js version, operating platform, architecture, working directory, and process ID. We also used command-line arguments, environment variables, process exit events, and exit codes in practical programs. These concepts help students at CIIT TRAINING INSTITUTE understand how Node.js applications interact with their runtime environment and prepare for the next topic: Command-Line Arguments.