Node.js – OS Module
Subject: nodejs
Node.js – OS Module
The os
(Operating System) module in Node.js provides built-in methods for retrieving information about the host machine’s OS. It’s useful for system-level diagnostics, CLI tools, and monitoring apps.
Importing the OS Module
Key OS Module Methods
1. os.platform()
Returns the OS platform.
2. os.arch()
Returns the CPU architecture.
3. os.cpus()
Returns details about each logical CPU core.
Includes model, speed (MHz), and user/system/idle times.
4. os.totalmem() and os.freemem()
Get total and available memory in bytes.
5. os.uptime()
System uptime in seconds.
6. os.hostname()
Gets the system’s hostname.
7. os.type()
Gets the operating system name.
8. os.release()
OS version release number.
9. os.userInfo()
Returns info about the current user.
10. os.networkInterfaces()
Returns network interface details.
11. os.tmpdir()
Returns the temporary files directory.
12. os.homedir()
Returns the user's home directory.
Real Example: Display System Report
Key Takeaways
- The
os
module provides essential system-level info. - Access platform, CPU, memory, network interfaces, and more.
- Great for diagnostics, performance monitoring, and system-aware apps.