Node.js Course
Node.js
/
Beginner

OS Module

Definition

A core module that provides operating system-related utility methods and properties. It allows Node.js to ask the computer about its hardware and OS.

Explain Like I'm New

It's the dashboard for your server's hardware. It lets your code peek outside of Node.js to check how much RAM the server has left, how many CPU cores it possesses, and whether it is running Windows, Linux, or Mac.

Real World Example

Writing a health-check API endpoint `/health` that returns the server's free memory and CPU uptime to a monitoring dashboard like Datadog.

Common Use Cases

  • •Server monitoring
  • •Spawning CPU Clusters
  • •Cross-platform logic adjustments

Interactive Example

Loading...
Console output will appear here...

Interview Questions

basic

  • How do you find out if the code is running on Windows or Mac?

intermediate

  • Why would you need to use `os.cpus().length`?

advanced

  • How do you get the server's total memory in Gigabytes?

Flash Cards

Question

How do you find the OS?

Click to reveal answer
Answer

Use `os.platform()`. It returns strings like `'win32'` (Windows), `'darwin'` (Mac), or `'linux'`.

Question

Why use os.cpus().length?

Click to reveal answer
Answer

Because Node is single-threaded, it only uses 1 CPU core natively. If you have an 8-core server, you are wasting 7 cores! You read `os.cpus().length` to automatically spin up 8 identical Node processes (Clustering) to maximize server hardware.