DevAcademy
LearnNode.jsThe os Module
BeginnerNode.js

The os Module

Query information about the operating system a Node.js process is running on.

Reading Time

8 min

Lesson

Lesson 9 of 34

What os Provides

The os module exposes details about the underlying operating system and hardware — CPU info, memory, network interfaces, and the current user — useful for diagnostics, logging, and platform-specific logic.

Common os Methods

Try it yourself — edit and run

Console Output

Click “Run” to see the console output here.

Common Uses

MethodUse Case
os.cpus().lengthDeciding how many worker processes to spawn
os.platform()Running platform-specific logic or file paths
os.totalmem() / os.freemem()Health checks and monitoring dashboards
os.tmpdir()A safe, OS-appropriate location for temporary files

Deciding Worker Count Based on CPU Cores

Try it yourself — edit and run

Console Output

Click “Run” to see the console output here.

Values Reflect the Host Machine

Inside a container (like Docker), os.cpus() and os.totalmem() report values based on the underlying host or container limits, which is worth remembering when using them to size worker pools in a containerized deployment.

Best Practice

Use os.cpus().length as a starting point for sizing a worker pool or process cluster, but validate it against your actual container resource limits in production rather than assuming it matches your deployment environment exactly.

Interview Questions

Quick Quiz

1. What does os.platform() return?

2. What is a common use for os.cpus().length?

3. What does os.tmpdir() return?