Whenever we build backend applications, there are situations where we need information about the operating system on which our application is running.
For example:
- Which operating system is the server using?
- How much RAM is available?
- What is the CPU architecture?
- How many CPU cores are available?
- What is the hostname of the machine?
Instead of using third-party packages, Node.js provides a built-in OS module to retrieve all this information.
In this article, we'll explore the most commonly used methods of the OS module with step-by-step examples.
By the end, you'll know how to use the OS module to gather system information in your Node.js applications.
Let's get started!
What is the OS Module?
The OS module is a built-in Node.js module that provides information about the operating system.
It allows developers to retrieve details such as:
- Operating System Name
- CPU Information
- Memory Usage
- System Architecture
- Hostname
- User Home Directory
- Network Interfaces
- System Uptime
Since it's a built-in module, there's no need to install anything.
Simply import it.
const os = require("os");
Or, if you're using ES Modules:
import os from "node:os";
Why Do We Need the OS Module?
Imagine you're deploying your application to multiple servers.
Before starting your application, you may want to know:
- Is the server Windows or Linux?
- How much RAM does it have?
- How many CPU cores are available?
- Which architecture is it running on?
The OS module provides all this information.
It's commonly used for:
- Server monitoring
- Performance analysis
- Logging system information
- Debugging
- Environment checks
Understanding os.platform()
What is os.platform()?
This method returns the platform on which the Node.js application is running.
Step 1: Create a File
Create a file named:
platform.js
Add the following code:
const os = require("os");
console.log(os.platform());
Run:
node platform.js
Example Output (Windows)
win32
Example Output (macOS)
darwin
Example Output (Linux)
linux
Explanation
win32→ Windowsdarwin→ macOSlinux→ Linux
Understanding os.arch()
What is os.arch()?
Returns the CPU architecture of the operating system.
Example:
const os = require("os");
console.log(os.arch());
Run:
node platform.js
Output:
x64
Possible values include:
- x64
- arm64
- ia32
Real-World Use Case
Some software packages are architecture-specific.
Before installing binaries, applications often check the system architecture.
Understanding os.hostname()
What is os.hostname()?
Returns the hostname of the current computer.
const os = require("os");
console.log(os.hostname());
Output:
DESKTOP-ABCD123
or
MacBook-Pro
Real-World Use Case
Useful for:
- Logging
- Monitoring servers
- Identifying machines in distributed systems
Understanding os.type()
What is os.type()?
Returns the operating system name.
const os = require("os");
console.log(os.type());
Output on Windows:
Windows_NT
Output on macOS:
Darwin
Output on Linux:
Linux
Understanding os.release()
Returns the operating system version.
const os = require("os");
console.log(os.release());
Example Output:
10.0.26100
or
24.5.0
This helps identify the OS version running on the machine.
Understanding os.homedir()
Returns the current user's home directory.
const os = require("os");
console.log(os.homedir());
Windows:
C:\Users\John
macOS:
/Users/john
Linux:
/home/john
Real-World Use Case
Applications often store configuration files inside the user's home directory.
Understanding os.tmpdir()
Returns the operating system's temporary directory.
const os = require("os");
console.log(os.tmpdir());
Example Output:
Windows
C:\Users\John\AppData\Local\Temp
Linux
/tmp
Real-World Use Case
Applications store temporary files here before processing or uploading them.
Understanding os.totalmem()
Returns the total amount of system memory in bytes.
const os = require("os");
console.log(os.totalmem());
Output:
17079869184
This value is in bytes.
To convert it into GB:
const os = require("os");
const totalMemory = os.totalmem();
console.log((totalMemory / 1024 / 1024 / 1024).toFixed(2), "GB");
Output:
15.91 GB
Understanding os.freemem()
Returns the available free memory.
const os = require("os");
const freeMemory = os.freemem();
console.log((freeMemory / 1024 / 1024 / 1024).toFixed(2), "GB");
Example Output:
6.34 GB
Real-World Use Case
Useful for monitoring server resources before executing memory-intensive tasks.
Understanding os.cpus()
Returns detailed information about each CPU core.
const os = require("os");
console.log(os.cpus());
Example Output:
[
{
model: "Intel(R) Core(TM) i7",
speed: 2600,
times: {
user: ...,
idle: ...
}
}
]
The returned array contains one object for each CPU core.
Count CPU Cores
const os = require("os");
console.log(os.cpus().length);
Output:
8
Real-World Use Case
Applications like web servers use this value to create worker processes using Node.js Cluster or Worker Threads.
Understanding os.uptime()
Returns the amount of time the operating system has been running.
const os = require("os");
console.log(os.uptime());
Output:
86520
This value is in seconds.
Convert it into hours.
const os = require("os");
const hours = (os.uptime() / 60 / 60).toFixed(2);
console.log(hours + " Hours");
Output:
24.03 Hours
Understanding os.userInfo()
Returns information about the currently logged-in user.
const os = require("os");
console.log(os.userInfo());
Example Output:
{
uid: 501,
gid: 20,
username: "john",
homedir: "/Users/john",
shell: "/bin/zsh"
}
Real-World Use Case
Useful for:
- Logging
- Debugging
- User-specific configurations
Understanding os.networkInterfaces()
Returns all available network interfaces.
const os = require("os");
console.log(os.networkInterfaces());
Example Output:
{
Ethernet: [
{
address: "192.168.1.5",
family: "IPv4"
}
]
}
Real-World Use Case
Useful for:
- Finding the machine's IP address
- Network diagnostics
- Local development tools
Build a System Information Application
Let's combine multiple methods into a simple application.
Create:
system-info.js
const os = require("os");
console.log("Platform:", os.platform());
console.log("Architecture:", os.arch());
console.log("Hostname:", os.hostname());
console.log("Operating System:", os.type());
console.log("OS Version:", os.release());
console.log("Home Directory:", os.homedir());
console.log(
"Total Memory:",
(os.totalmem() / 1024 / 1024 / 1024).toFixed(2),
"GB"
);
console.log(
"Free Memory:",
(os.freemem() / 1024 / 1024 / 1024).toFixed(2),
"GB"
);
console.log("CPU Cores:", os.cpus().length);
console.log("System Uptime:", (os.uptime() / 3600).toFixed(2), "Hours");
Run:
node system-info.js
Example Output:
Platform: win32
Architecture: x64
Hostname: DESKTOP-12345
Operating System: Windows_NT
OS Version: 10.0.26100
Home Directory: C:\Users\John
Total Memory: 15.91 GB
Free Memory: 6.42 GB
CPU Cores: 8
System Uptime: 24.14 Hours
This simple application provides a quick overview of your system's hardware and operating system details.
Comparison of Common OS Module Methods
os.platform() - Returns the operating system platform
os.arch() - Returns the CPU architecture
os.type() - Returns the operating system name
os.release() - Returns the operating system version
os.hostname() - Returns the computer's hostname
os.homedir() - Returns the user's home directory
os.tmpdir() - Returns the temporary directory
os.totalmem() - Returns the total system memory
os.freemem() - Returns the available memory
os.cpus() - Returns information about CPU cores
os.uptime() - Returns system uptime
os.userInfo() - Returns details about the current user
os.networkInterfaces() - Returns network interface information
Best Practices
When using the OS module:
- Use it for retrieving system information only.
- Convert memory values from bytes into MB or GB for better readability.
- Avoid calling
os.cpus()repeatedly inside performance-critical loops, as it returns detailed information about each core. - Don't rely on platform-specific values (
win32,darwin, etc.) without handling other operating systems. - Use the OS module for diagnostics, monitoring, and environment detection.
Common Beginner Mistakes
Here are some common mistakes developers make:
- Forgetting to import the OS module before using it.
- Assuming memory values are returned in MB or GB (they are returned in bytes).
- Confusing
os.platform()withos.type(). - Assuming every operating system returns identical values.
- Logging the full
os.cpus()output unnecessarily when only the number of cores is needed.
Final Thoughts
The OS module is one of the simplest yet most useful built-in modules in Node.js. It provides valuable information about the system your application is running on without requiring any external dependencies.
In this article, you learned:
- What the OS module is
- How to retrieve operating system information
- How to check CPU architecture
- How to retrieve memory statistics
- How to get hostname, home directory, and temporary directory
- How to inspect CPU details
- How to retrieve network interfaces
- How to build a simple system information application
Although you may not use the OS module every day, it's incredibly useful for server-side applications, monitoring tools, deployment scripts, and debugging utilities. Understanding these methods will help you write applications that are more aware of their runtime environment.
Happy Coding! 🚀