Node.js Tutorial

Node.js Process Object

Node.js Process Object
Node.js Process Object

Node.js Process Object

In Node.js, the process object is a powerful global utility that provides information about the currently running application. It gives developers access to details such as the process ID, processor architecture, operating system platform, Node.js version, uptime, memory usage, and environment variables.

The process object can also be used to control the application lifecycle, terminate processes, handle signals, and respond to process events. Since it is global, you can access it anywhere in a Node.js application without importing a module.

Common Process Properties

Complete Advance AI Topics: Click Here
SQL Tutorial:
Click Here

Node.js provides several useful properties through the process object:

  • arch – Returns the processor architecture, such as arm, ia32, or x64.
  • argv – Contains the command-line arguments passed to the Node.js process.
  • env – Provides access to environment variables.
  • pid – Returns the process ID of the current Node.js application.
  • platform – Returns the operating system platform, such as darwin, linux, or win32.
  • version – Returns the current Node.js version.
  • versions – Provides version information for Node.js and its dependencies.

Example 1: Basic Process Properties

The following example displays some basic information about the running Node.js process:

// process_example1.js
console.log(`Architecture: ${process.arch}`);
console.log(`PID: ${process.pid}`);
console.log(`Platform: ${process.platform}`);
console.log(`Version: ${process.version}`);

When you run this program, it displays the processor architecture, process ID, operating system platform, and Node.js version.

Example 2: Command-Line Arguments

The process.argv property is used to access arguments provided through the command line.

// process_example2.js
process.argv.forEach((value, index) => {
  console.log(`${index}: ${value}`);
});

When a Node.js application is executed, process.argv contains an array of command-line arguments. The Node.js executable is generally the first argument, the JavaScript file is the second, and any additional arguments follow them.

Useful Process Functions

The process object also provides several useful functions for working with the running application:

  • cwd() – Returns the current working directory.
  • hrtime() – Provides high-resolution real-time information in seconds and nanoseconds.
  • memoryUsage() – Returns information about the memory usage of the Node.js process.
  • kill(pid[, signal]) – Sends a signal to a process identified by its process ID.
  • uptime() – Returns the amount of time the Node.js process has been running, in seconds.

Example 3: Current Directory and Process Uptime

// process_example3.js
console.log(`Current directory: ${process.cwd()}`);
console.log(`Uptime: ${process.uptime()} seconds`);

This example displays the current working directory and the amount of time for which the Node.js process has been running.

Handling Process Events

The process object is also an EventEmitter, which means you can listen for important process-related events. For example, the exit event can be used to perform actions when a Node.js process is about to terminate.

process.on('exit', (code) => {
  console.log(`Process exiting with code: ${code}`);
});

The callback receives the process exit code, which can help identify whether the application terminated normally or with an error status.

Download New Real Time Projects:- Click here

Conclusion

The Node.js process object is an essential part of Node.js development. It provides access to runtime information such as process ID, platform, architecture, command-line arguments, environment variables, memory usage, and uptime. It also allows developers to handle process events and control the application lifecycle.

Understanding the process object is important for building and managing Node.js applications effectively.

Keywords

Node.js Process Object, Node.js process, process object in Node.js, Node.js process properties, process.argv, process.env, process.pid, process.platform, process.arch, process.version, Node.js process functions, process.cwd(), process.uptime(), process.memoryUsage(), process.kill(), Node.js process events

Source Code Available

Interested in This Project?

Get the complete source code for this project at a very affordable price — perfect for your portfolio, college submission, or learning. Message us on WhatsApp and we'll get back to you instantly!

Full source code included Step-by-step setup guide Instant delivery on WhatsApp Instant reply on WhatsApp
Chat on WhatsApp

We usually reply within a few minutes

Leave a Reply

Your email address will not be published. Required fields are marked *

Chat with us