← Back to Node.js Course | Chapter 2: Core Modules | Lesson 3 of 7

os module

The os module tells you about the computer your program is running on.

In this page:

  1. os module
Syntax
javascript
const os = require('os');
os.platform()
os.cpus().length
os.totalmem()
os.homedir()

os module

os exposes the platform, CPU architecture, CPU count, memory, home directory, temp directory and line ending. It is useful for writing cross-platform tools and diagnostics.

Note: Use os.EOL for line endings when writing text files.

Example: os module

javascript
const os = require("os");
console.log("platform is string:", typeof os.platform());
console.log("cpus > 0:", os.cpus().length > 0);
console.log("total >= free memory:", os.totalmem() >= os.freemem());
console.log("EOL is newline:", os.EOL === "\n" || os.EOL === "\r\n");

// Output:
// platform is string: string
// cpus > 0: true
// total >= free memory: true
// EOL is newline: true

⚠️ Run this in your own terminal or Node.js environment.

Related Topics
Common Mistakes
  1. Assuming the platform is Linux
  2. Hard-coding /tmp instead of os.tmpdir()
  3. Confusing total and free memory
Chapter Summary
  • os describes the host machine
  • platform() and arch() identify the system
  • tmpdir() and homedir() give paths
  • EOL is the line ending
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.