Node.js — OS
Import OS Module
const os = require('os');
Basic Information
os.platform() // 'win32', 'darwin', 'linux'
os.arch() // 'x64', 'arm64'
os.hostname() // Computer name
os.type() // 'Windows_NT', 'Darwin', 'Linux'
os.release() // OS version
os.version() // Kernel version
System Resources
// Total memory
os.totalmem() // In bytes
// Free memory
os.freemem() // In bytes
// CPUs
os.cpus() // Array of CPU info
// Uptime
os.uptime() // In seconds
Network Interfaces
const interfaces = os.networkInterfaces();
console.log(interfaces);
Home Directory
os.homedir() // User home directory
os.tmpdir() // Temporary directory
Practical Example
function getSystemInfo() {
return {
platform: os.platform(),
arch: os.arch(),
memory: {
total: os.totalmem(),
free: os.freemem()
},
cpus: os.cpus().length
};
}
Mini Practice
- Get system platform
- Check available memory
- Get CPU information
- Find home directory
Up Next
Continue with URL — URL parsing and manipulation.
Related Topics
Frequently Asked Questions about OS
What is OS in Node.js?
OS is a fundamental concept in Node.js. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn OS?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn OS.
Why is OS important in Node.js?
OS is essential for Node.js development. Understanding this concept will help you write better code and solve real-world problems more effectively.