Accessing Node.js Process Information Using the `process` Object

Learn how to access and utilize information about the current Node.js process using the built-in `process` object. This tutorial covers key `process` object properties (pid, version, platform, etc.) and methods (exit, on, etc.) for managing and monitoring your Node.js applications.



Accessing Node.js Process Information

Node.js provides a built-in `process` object that gives you access to various aspects of the current Node.js process. This includes information about the process itself (ID, version, platform, etc.) and methods for managing the process (like terminating it).

Node.js Process Properties

The `process` object has many properties; here are some key ones:

Property Description
process.arch The system's architecture (e.g., 'x64', 'arm').
process.argv An array of command-line arguments.
process.env An object containing environment variables.
process.pid The process ID (PID) of the Node.js process.
process.platform The operating system platform (e.g., 'linux', 'win32', 'darwin').
process.release Metadata about the Node.js release.
process.version The Node.js version.
process.versions An object with Node.js and its dependencies' versions.

Example 1: Accessing Basic Process Information


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

Example 2: Accessing Command-Line Arguments


process.argv.forEach((val, index) => {
  console.log(`${index}: ${val}`);
});

Node.js Process Methods

Method Description
process.cwd() Gets the current working directory.
process.hrtime() Gets high-resolution real-time.
process.memoryUsage() Gets memory usage information.
process.kill(pid[, signal]) Terminates a process.
process.uptime() Gets the process uptime (in seconds).

Example 3: Current Directory and Uptime


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