13) Node OS Module
Introduction
Theosmodule in Node.js is a core module that provides information about the operating system (OS) your Node application is running on. It helps you write programs that are aware of system-level details like CPU, memory, platform, and more.osis a core Node.js module
Provides system-level information
Common methods:platform(),arch(),cpus() totalmem(),freemem()hostname(),uptime()
Used for:
Performance monitoring
Scaling apps
Environment detectionWhy Use the
osModule?
You use it when you need:
System information (CPU, memory)
Platform-specific logic
Performance monitoring
Debugging or logging environment detailsCommonly Used Methods
i) OS Platformconsole.log(os.platform()); // Output // win32 // linux // darwin (Mac)ii) CPU Architecture
console.log(os.arch()); // x64, armiii) CPU Info
console.log(os.cpus()); /* Returns array of CPU cores with: model speed times (user, idle, etc.) */iv) Total Memory
console.log(os.totalmem()); // in bytesv) Free Memory
console.log(os.freemem());vi) Hostname
console.log(os.hostname());vii) OS Type
console.log(os.type()); // Linux // Windows_NTviii) Example
const os = require('os'); console.log('Platform:', os.platform()); console.log('CPU Arch:', os.arch()); console.log('Total Memory:', os.totalmem()); console.log('Free Memory:', os.freemem()); console.log('CPUs:', os.cpus().length);Real-World Use Cases
i) Performance Monitoring
Check memory usage
CPU load
ii) Environment Detectionif (os.platform() === 'win32') { console.log('Running on Windows'); }iii) Scaling Apps
Decide number of workers based on CPU coresconst numCPUs = os.cpus().length;iv) Logging & Debugging
Capture system details in logs