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.
On this page · 5 min · Beginner
On this page
- Learning objectives
- Explanation
- Syntax
- Code example
- Step by step
- Real-world use
- Common mistakes
- Best practices
- Remember this
- Practice exercise
- Mini quiz
- Related topics
- My notes
- Reading time
- 5 min
- Difficulty
- Beginner
- Updated
- Oct 28, 2026
Related tutorials
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#
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#
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');
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#
- platform and arch identify the OS and processor type.
- availableParallelism counts usable CPU threads.
- freemem and totalmem are converted from bytes to GB.
- process.memoryUsage().rss shows this process’s memory use.
- 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#
They differ on Windows. Use os.EOL and os.tmpdir().
Exposing hostname, versions and memory on a public endpoint helps attackers. Protect health details or keep them minimal.
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?
Show solution
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#
What does os.platform() return on Windows?
Want more? Practise all Node.js MCQs · Node.js interview questions
My notes#
Saved in this browser.