Beginner Fundamentals

The os Module

The os module gives you information about the operating system and the machine your program runs on, such as the platform, CPUs, and memory.

Platform and architecture

const os = require("os");

console.log(os.platform()); // 'darwin', 'linux', or 'win32'
console.log(os.arch());     // 'x64', 'arm64', etc.

CPU information

os.cpus() returns an array, one entry per logical core:

const os = require("os");

const cpus = os.cpus();
console.log("Number of cores:", cpus.length);
console.log("Model:", cpus[0].model);

Memory

const os = require("os");

console.log("Free memory:", os.freemem());
console.log("Total memory:", os.totalmem());

The values are in bytes. Divide by 1024 * 1024 * 1024 to get gigabytes.

Other helpers

  • os.hostname() returns the machine name.
  • os.homedir() returns the user’s home folder.
  • os.uptime() returns seconds since boot.

Summary

Use the os module when you need details about the environment, like choosing how many workers to start based on os.cpus().length.