Core Modules
Node.js os and process Modules
The os module reports information about the machine — CPUs, memory, platform, uptime. process describes the running Node process and lets you read environment variables, handle signals and exit cleanly.
What is os and process Modules in Node.js?
The os module reports information about the machine — CPUs, memory, platform, uptime. process describes the running Node process and lets you read environment variables, handle signals and exit cleanly.
os and process Modules example
JavaScript
import os from 'node:os';
os.cpus().length; // logical CPU count — used to size a cluster
os.totalmem() / 1e9; // total RAM in GB
os.freemem();
os.platform(); // 'linux' | 'darwin' | 'win32'
os.hostname();
os.tmpdir();
process.memoryUsage().heapUsed; // bytes of heap in use
process.uptime(); // seconds since start
process.cwd(); // current working directoryKey points to remember
- os.cpus().length decides how many workers a cluster or PM2 should run.
- process.memoryUsage() is the first thing to check when a leak is suspected.
- os.tmpdir() gives the correct temporary directory on every platform.
Node.js os and process Modules— Interview Questions & FAQs
How do I find out how many CPU cores Node can use?+
os.cpus().length returns the number of logical cores, which is the usual number of worker processes to run under cluster or PM2 in cluster mode.
