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 which 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

To access CLI arguments, we use the process.argv property.

The first two elements are ‘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 a format:
{
  rss: 22790144,
  heapTotal: 6037504,
  heapUsed: 3818032,
  external: 1583000,
  arrayBuffers: 9461
}