Skip to the solution
EasyPhone Screen5 min read

What is the use of the 'os' module in Node.js?

418 views

The problem

Question presented to candidate: "Your app needs to decide how many worker processes to spawn based on the machine it is running on, and needs a safe place to write a temporary file. What built-in module gives you that information?"

What a strong answer should cover:

  • Node's built-in os module exposes operating-system-level information: CPU count/details, total and free memory, platform identifier, network interfaces, the user's home directory, and the system's temp directory.
  • os.cpus() returns an array with one entry per logical CPU core, and .length is the standard way application code decides how many worker processes/threads to spawn for CPU-bound parallelism (feeding directly into the cluster module or a Worker Thread pool, both covered in their own dedicated questions).
  • os.totalmem()/os.freemem() report memory in bytes, for the whole machine β€” not the current Node process's own memory usage, which is a separate concern (process.memoryUsage()), a commonly conflated pair.
  • os.tmpdir() gives the correct, platform-appropriate temporary directory β€” critically, this is not a fixed path; it varies by OS and even by user account, and hardcoding /tmp (a POSIX-only assumption) breaks on Windows.
  • os.platform() returns a specific identifier ("win32", "darwin", "linux", etc.) β€” the standard, correct way to branch on operating system, rather than inferring it indirectly from something like a path separator.
  • os.EOL gives the platform's correct line-ending sequence (\n on POSIX, \r\n on Windows) β€” relevant when generating text output meant to look correct when opened in a platform-native text editor.

Clarifying questions expected:

  • "Is the concern the whole machine's resources, or this specific Node process's own usage?" β€” os reports the former; process reports the latter.
  • "Does the code need to run correctly across multiple operating systems, or only one known target?" β€” decides how much of os's cross-platform value actually matters here.

Code / implementation expected: Optional β€” reading real values directly from os on the actual running machine is a clean, concrete way to ground the answer rather than describing the module abstractly.

  • os
  • modules
  • performance

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

5 min read Β· 8 sections

Read the code

JavaScriptReal os module values read directly from this running machine
const os = require("os");

console.log(os.platform());                     // win32
console.log(os.cpus().length);                   // 20
console.log((os.totalmem() / 1e9).toFixed(1));    // 34.0 (GB)
console.log((os.freemem() / 1e9).toFixed(1));     // 15.8 (GB)
console.log(os.homedir());                        // C:\Users\arvin
console.log(os.tmpdir());                         // C:\Users\arvin\AppData\Local\Temp
console.log(JSON.stringify(os.EOL));               // "\r\n"

// A real, common use: deciding how many workers to spawn
const numWorkers = os.cpus().length;

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 23 of 152 in Node.js

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

All Node.js questions
Up nextEasy

What is `npm` and what are its core functionalities?

Continue