Node.js · Modules

OS Module

Sometimes your code needs to know about the machine it runs on: how many CPUs to use, how much memory is free, where the temp folder is. The os module answers those questions.

Beginner 5 min Lesson 9 of 37Examples target Node.js 22 LTS.
Before you start:Modules
0/37 in Node.js
On this page · 5 min · Beginner

Learning objectives#

  • Read platform and architecture
  • Check CPUs and memory
  • Find home and temp directories and the line ending
  • Build a simple health-check report

Explanation#

os.platform() returns "win32", "linux" or "darwin"; os.arch() returns "x64" or "arm64"; os.release() gives the OS version.

os.availableParallelism() (or os.cpus().length) tells you how many CPU threads you can use — handy for sizing worker pools or cluster processes.

os.totalmem() and os.freemem() return bytes. process.memoryUsage().rss is the memory your own Node process uses.

os.homedir() and os.tmpdir() give standard folders; os.EOL is the line ending ("\r\n" on Windows, "\n" elsewhere); os.hostname() names the machine.

os.uptime() is how long the machine has been up; process.uptime() how long your process has run. Both are useful in /health endpoints.

Syntax#

JavaScript Illustrative
import os from 'node:os';
os.platform();               // 'linux'
os.availableParallelism();   // 8
os.freemem() / 1024 ** 3;    // free GB
os.tmpdir();                 // '/tmp'

Code example: A machine health report#

JavaScripthealth.js Illustrative
import os from 'node:os';


const gb = bytes => (bytes / 1024 ** 3).toFixed(1) + ' GB';
const mb = bytes => (bytes / 1024 ** 2).toFixed(0) + ' MB';


const report = {
  platform: `${os.platform()} ${os.arch()}`,
  cpus: os.availableParallelism(),
  memory: `${gb(os.freemem())} free of ${gb(os.totalmem())}`,
  processMemory: mb(process.memoryUsage().rss),
  uptimeHours: (os.uptime() / 3600).toFixed(1),
  tempDir: os.tmpdir(),
  lineEnding: JSON.stringify(os.EOL),
};


console.table(report);


const lowMemory = os.freemem() / os.totalmem() < 0.1;
console.log(lowMemory ? '⚠ Less than 10% memory free' : '✓ Memory OK');
Expected output
Prints a table such as: platform “linux x64”, cpus 8, memory “5.2 GB free of 15.6 GB”, processMemory “42 MB”, uptimeHours “73.4”, tempDir “/tmp”, lineEnding "\n" — the values vary by machine — followed by “✓ Memory OK” (or a warning when less than 10% is free).

This example is illustrative. Copy or download it and run it in your own environment; the expected output is shown above.

Step-by-step explanation#

  1. platform and arch identify the OS and processor type.
  2. availableParallelism counts usable CPU threads.
  3. freemem and totalmem are converted from bytes to GB.
  4. process.memoryUsage().rss shows this process’s memory use.
  5. console.table prints the object as a readable table.

Real-world use case#

An API’s /health endpoint returns uptime, memory and CPU count; the monitoring system alerts when free memory drops below 10% or the process restarts unexpectedly.

Common mistakes#

Hard-coding "\n" or "/tmp"

They differ on Windows. Use os.EOL and os.tmpdir().

Leaking machine details publicly

Exposing hostname, versions and memory on a public endpoint helps attackers. Protect health details or keep them minimal.

Confusing machine and process memory

freemem is the whole machine; process.memoryUsage is your app. Watch both.

Best practices#

  • Use os for platform-specific values
  • Size worker pools with availableParallelism()
  • Expose health info only to monitoring
  • Log memory trends, not just snapshots

Remember this#

platform / arch / availableParallelism.

freemem & totalmem are in bytes.

os.EOL and os.tmpdir() instead of hard-coding.

Practice exercise#

Your task: Print "Running on <platform> with <n> CPUs" and warn if fewer than 2 CPUs are available.

Need a hint?
os.platform() and os.availableParallelism().
Show solution
JavaScript Illustrative
import os from 'node:os';


const cpus = os.availableParallelism();
console.log(`Running on ${os.platform()} with ${cpus} CPUs`);
if (cpus < 2) console.log('Warning: this machine has fewer than 2 CPUs');

Mini quiz#

OS Module quizMultiple choice Question 1 of 3

What does os.platform() return on Windows?

Score so far: 0/0

Want more? Practise all Node.js MCQs · Node.js interview questions

My notes#

Saved in this browser.

Progress is saved in this browser. Sign in to sync.