os module
The os module tells you about the computer your program is running on.
In this page:
Syntax
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
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
- Assuming the platform is Linux
- Hard-coding /tmp instead of os.tmpdir()
- 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: