Hiprup

What is the purpose of the process object in Node.js?

The process object is a global object that provides information about — and control over — the currently running Node.js process. It's the main interface between your app and the runtime environment.

Common uses:

  • process.env — access environment variables (e.g., process.env.NODE_ENV).

  • process.argv — read command-line arguments.

  • process.cwd() — get the current working directory.

  • process.exit(code) — terminate the process with an exit code.

  • process.pid — current process ID.

  • process.platform / process.arch — OS and CPU info.

  • process.memoryUsage() — memory stats for monitoring.

  • process.uptime() — how long the process has been running.

  • process.stdin / stdout / stderr — standard I/O streams.

Event handling:

  • process.on('exit') — cleanup before shutdown.

  • process.on('uncaughtException') — catch unhandled errors.

  • process.on('unhandledRejection') — catch unhandled promise rejections.

  • process.on('SIGINT' / 'SIGTERM') — handle graceful shutdown (Ctrl+C, Docker stop).

// Environment and system info
console.log(process.env.NODE_ENV);  // 'production'
console.log(process.pid);           // 54321
console.log(process.cwd());         // /app

// Memory usage
const mem = process.memoryUsage();
console.log(`Heap: ${Math.round(mem.heapUsed / 1024 / 1024)}MB`);

// Graceful shutdown
process.on('SIGTERM', () => {
  console.log('Received SIGTERM, shutting down gracefully');
  server.close(() => {
    process.exit(0);
  });
});

This demonstrates three common uses of the process object: reading environment variables and system info, monitoring memory usage by converting bytes to megabytes, and handling the SIGTERM signal for graceful shutdown by closing the server before exiting.

Focus on practical uses: environment variables, graceful shutdown with signal handling, and memory monitoring. These show production-level awareness.

What is the purpose of the process object in Node.js? | Hiprup