Skip to main content

What is process?

1. What process is

process is an object provided by Node.js that describes the currently running JavaScript process in the Node.js environment.

It's available anywhere in the program, with no need to import it:

javascript
console.log(process);

An analog in other languages:

  • in Python, sys + os;
  • in C, getpid(), argv, exit(), and so on.

2. Main capabilities of process

CategoryWhat it does
Process informationPID, Node.js version, architecture, platform
Environment variablesprocess.env
Launch argumentsprocess.argv
Standard streamsprocess.stdin, process.stdout, process.stderr
Ending the processprocess.exit()
Lifecycle eventsbeforeExit, exit, uncaughtException, SIGINT
Resource informationMemory, CPU, uptime, the current directory
Node.js flags and optionsprocess.execPath, process.execArgv

3. Main properties and methods of process

process.pid

→ The ID of the current process (Process ID)

javascript
console.log(process.pid); // for example, 9342

process.version

→ The Node.js version

javascript
console.log(process.version); // v22.8.0

process.platform

→ Which platform Node.js is running on

javascript
console.log(process.platform); // 'win32' | 'linux' | 'darwin'

process.arch

→ The processor architecture

javascript
console.log(process.arch); // 'x64', 'arm64', 'ia32', etc.

process.cwd()

→ The current working directory

javascript
console.log(process.cwd()); // /Users/tim/projects/app

process.env

→ An object holding all environment variables

javascript
console.log(process.env.NODE_ENV); // 'production' console.log(process.env.PATH); // the system PATH

Used for configuration:

javascript
if (process.env.NODE_ENV === 'production') { console.log('Running in production'); }

process.argv

→ The command-line arguments Node.js was started with

javascript
node app.js dev 8080
javascript
console.log(process.argv); // [ '/usr/local/bin/node', '/path/to/app.js', 'dev', '8080' ]

Often used for CLI tools.

process.memoryUsage()

→ Shows how much memory the app is using

javascript
console.log(process.memoryUsage()); /* { rss: 24576000, heapTotal: 5464064, heapUsed: 3661728, external: 8704 } */

process.uptime()

→ Time (in seconds) since the process started

javascript
console.log(process.uptime()); // 5.312

process.exit([code])

→ Ends the process

javascript
process.exit(0); // a normal exit process.exit(1); // an exit with an error

Code 0 means success, any other code means an error (for example, used in CI/CD or shell scripts).

process.on(event, handler)

→ Subscribing to process events

Examples:

When the program ends:

javascript
process.on('exit', (code) => { console.log('Exiting with code:', code); });

An uncaught error:

javascript
process.on('uncaughtException', (err) => { console.error('Uncaught error:', err); });

When Ctrl+C is pressed:

javascript
process.on('SIGINT', () => { console.log('Process interrupted by user'); process.exit(); });

process.stdin, process.stdout, process.stderr

→ Input/output streams

javascript
process.stdout.write('Enter your name: '); process.stdin.on('data', (data) => { console.log(`Hello, ${data.toString().trim()}!`); });

This is a basic way to work with the console with no external libraries.

4. An example in a real Node.js application

javascript
console.log(`Started with PID: ${process.pid}`); console.log(`OS: ${process.platform}`); console.log(`Node.js: ${process.version}`); console.log(`Memory: ${(process.memoryUsage().heapUsed / 1024 / 1024).toFixed(2)} MB`); process.on('exit', (code) => { console.log(`Process exited with code ${code}`); }); setTimeout(() => { console.log('Exiting in 2 seconds...'); process.exit(0); }, 2000);

5. Why process matters

  • It lets you interact with the operating system;
  • it manages the environment and configuration (through env);
  • it gives access to CLI parameters;
  • it manages input/output streams;
  • it lets you catch errors and exit cleanly.

6. Common use cases

ScenarioUsed for
Environment configurationprocess.env.NODE_ENV
CLI applicationsprocess.argv
Debugging and loggingprocess.memoryUsage(), process.uptime()
Ending the processprocess.exit()
Error handlingprocess.on('uncaughtException', handler)
Graceful shutdownprocess.on('SIGINT', cleanup)

7. Quick summary

Property / MethodPurpose
process.pidThe process ID
process.platformThe OS platform
process.envEnvironment variables
process.argvLaunch arguments
process.cwd()The current directory
process.memoryUsage()Memory usage
process.uptime()How long the process has run
process.exit([code])Ending the process
process.on(event, cb)Subscribing to process events
process.stdin / stdout / stderrInput/output streams

In one sentence:

process is Node.js's global object that gives access to information about the running process, environment variables, command-line arguments, input/output streams, and control over how the program exits.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.