π¦ The os Module
os is a built-in Node.js module that gives you information about the computer your code is running on β the operating system, CPU, memory, and more. It's read-only: you use it to check system info, not change it.
const os = require('os');π₯οΈ Platform & System Type
console.log(os.platform()); // 'win32', 'darwin', or 'linux'
console.log(os.type()); // 'Windows_NT', 'Darwin', or 'Linux'
console.log(os.arch()); // 'x64', 'arm64', etc.
console.log(os.release()); // OS version numberplatform()gives a short OS code β'win32'for Windows,'darwin'for Mac,'linux'for Linux.type()gives the OS's internal kernel name instead β useful whenplatform()isn't specific enough.arch()tells you the CPU architecture β matters if your app depends on native binaries.release()gives the OS version, e.g. a specific Windows build or macOS version number.
βοΈ CPU Info
console.log(os.cpus());Output (shortened):
[
{
model: 'Apple M2',
speed: 3200,
times: { user: 123456, nice: 0, sys: 45678, idle: 987654, irq: 0 }
},
// ...one entry per CPU core
]cpus()returns an array with one object per core β model name, clock speed, and time spent in each state.os.cpus().lengthis a quick way to get the number of cores available.
πΎ Memory Info
console.log(os.totalmem()); // total RAM, in bytes
console.log(os.freemem()); // free RAM, in bytesBoth return raw bytes, so they're usually converted to something readable:
const totalGB = (os.totalmem() / (1024 ** 3)).toFixed(2);
const freeGB = (os.freemem() / (1024 ** 3)).toFixed(2);
console.log(`${freeGB} GB free of ${totalGB} GB total`);Handy for scripts that need to check available memory before doing something heavy, or for a status dashboard.
π€ User & Home Info
console.log(os.homedir()); // '/Users/bhaskar' or 'C:\\Users\\Bhaskar'
console.log(os.hostname()); // the computer's network name
console.log(os.userInfo()); // { username, uid, gid, shell, homedir }homedir()gives the current user's home folder β useful for finding config files that live outside your project.hostname()returns the machine's name on the network.userInfo()bundles several user details into one object, including the OS username running the script.
β±οΈ Uptime
const seconds = os.uptime();
const hours = (seconds / 3600).toFixed(1);
console.log(`System has been up for ${hours} hours`);uptime()returns how long the machine has been running, in seconds since it was last started β not how long your script has been running.
β©οΈ Line Endings
console.log(os.EOL === '\n'); // true on Mac/Linux, false on Windowsos.EOLis the correct line-break character for the current OS β\non Mac/Linux,\r\non Windows.Use it instead of hardcoding
\nwhen writing text files, so line breaks stay correct across platforms.
π‘ Why Use os?
Lets your app adapt to the machine it's running on β different file paths, different CPU counts, different line endings.
Common uses: system-info dashboards, performance/logging tools, cross-platform CLI apps, and scripts that scale their work based on available CPU cores or memory.
const os = require('os');
console.log(`Platform: ${os.platform()}`);
console.log(`CPU cores: ${os.cpus().length}`);
console.log(`Free memory: ${(os.freemem() / (1024 ** 3)).toFixed(2)} GB`);