Node.js Process

Each Node.js script that runs is a process.

Useful process information includes:

  • env: Environment variables
  • argv: Command-line arguments
  • exit(): Method to exit/terminate process
  • memoryUsage(): returns the memory usage of the Node.js process measured in bytes.

Check Node.js Process Documentation for more.

Environment Variables

The process.env property is an object that stores and controls information about the environment in which the process is currently running.

console.log(process.env)

{ SHELL: '/bin/bash',
  USER: 'anna',
  HOME: '/home/anna',
  LOGNAME: 'anna',
  ...
}

Command-Line Arguments

We use the process.argv property to access CLI arguments.

The first two elements are the node and the application’s name, while the rest are the command-line arguments.

node web.js testing app features
console.log(process.argv[2])
// Output: features

Exiting a Process

To exit a process, use the exit function:

process.exit()

process.memoryUsage()

process.memoryUsage() returns information on the CPU demands of the current process.

> process.memoryUsage()
//using process.memoryUsage() will return an object in the format:
{
  rss: 22790144,
  heapTotal: 6037504,
  heapUsed: 3818032,
  external: 1583000,
  arrayBuffers: 9461
}