The os Module Overview
You'll learn how the built-in os module gives Node.js programs access to information about the operating system they're running on.
What is the os Module?
The os module is a built-in Node.js module that acts like a window into your computer's system information. While the fs module lets you work with files and path helps you handle file paths, os tells you about the environment your code is running in—what kind of computer, how much memory is available, which operating system, and more.
Think of it like checking your computer's "About This System" panel, but programmatically from your JavaScript code.
Why You Need System Information
Sometimes your Node.js application needs to behave differently based on the system it's running on. Maybe you want to:
- Check how many CPU cores are available before spawning worker processes
- Detect whether you're on Windows, macOS, or Linux to adjust behavior
- Monitor available memory before starting memory-intensive operations
- Get the system's hostname or network interfaces
Basic Usage
Like other built-in modules, you import os using require():
const os = require('os');
console.log(os.platform()); // 'darwin', 'win32', 'linux', etc.
console.log(os.arch()); // 'x64', 'arm64', etc.
console.log(os.cpus().length); // Number of CPU cores
The os module is purely informational—it reads system data but doesn't modify anything. It's read-only access to your system's characteristics.
Key Takeaway: The os module provides a safe, cross-platform way to access system information like platform type, CPU architecture, and hardware details—essential for writing Node.js code that adapts to its environment.