os module

Last Updated : Oct 2026


The os module lets you inspect CPU cores, memory limits, and system details. This is useful when optimizing apps, managing background workers, or setting up health-check endpoints.

Import the os module using CommonJS:

const os = require("node:os");

Import the os module using ESM :

import os from "node:os";

Some common methods in the os module are:

  • os.platform(): Returns the name of the current operating system.
console.log("OS Platform:", os.platform()); // e.g., 'win32', 'darwin', 'linux'
  • os.arch(): Returns the architecture of the current machine.
console.log("OS Architecture:", os.arch()); // e.g., 'x64', 'arm64'
  • os.cpus(): Returns an array of CPU information.
const cpus = os.cpus();
console.log("Number of CPUs:", cpus.length);
console.log("CPU Model:", cpus[0].model);
  • os.totalmem(): Returns the total amount of memory in bytes.
const totalMemory = os.totalmem();
console.log("Total Memory:", totalMemory); // in bytes
console.log("Total Memory (GB):", (totalMemory / 1024 ** 3).toFixed(2)); // in GB
  • os.freemem(): Returns the amount of free memory in bytes.
const freeMemory = os.freemem();
console.log("Free Memory:", freeMemory); // in bytes
console.log("Free Memory (GB):", (freeMemory / 1024 ** 3).toFixed(2)); // in GB
  • os.homedir(): Returns the user's home directory.
const homeDir = os.homedir();
console.log("Home Directory:", homeDir);

Liked This Tutorial

Scan QR Code To Leave A Review Or

Click Here To Leave A Review
Scan QR