Node JS OS Module
Written By: Avinash Malhotra
Updated on
OS Modules
os module is used to get operating system related properties. This includes os architecture, RAM, number of processors, network interfaces, os platform, os type etc.
OS Modules is a build in module in node js.
OS Modules Code
CJS Module
const os=require('os');
ESM Module
import * as os from 'os';
OS Architecture
os.arch method returns the architecture of operating system. The possible values are 'arm', 'arm64', 'ia32', 'mips', 'mipsel', 'ppc', 'ppc64', 's390', 's390x', 'x32', and 'x64'.
const os=require('os');
const arch=os.arch();
OS CPUS
os.cpus method returns the information of each CPU Core. This method returns an array of objects having information about each CPU Core.
[
{
model: 'Intel(R) Core(TM) i5-5250U CPU @ 1.60GHz',
speed: 1600,
times: { user: 9886220, nice: 0, sys: 5667630, idle: 53688060, irq: 0 }
},
{
model: 'Intel(R) Core(TM) i5-5250U CPU @ 1.60GHz',
speed: 1600,
times: { user: 3471770, nice: 0, sys: 2076480, idle: 63685880, irq: 0 }
},
{
model: 'Intel(R) Core(TM) i5-5250U CPU @ 1.60GHz',
speed: 1600,
times: { user: 9939290, nice: 0, sys: 4517010, idle: 54777840, irq: 0 }
},
{
model: 'Intel(R) Core(TM) i5-5250U CPU @ 1.60GHz',
speed: 1600,
times: { user: 3209900, nice: 0, sys: 1871270, idle: 64152950, irq: 0 }
}
]
const os=require('os');
const cpus=os.cpus();
To get information of each individual cpu core, use for each method of above array.
const os=require('os');
const cpus=os.cpus();
cpus.forEach(function(i){
console.log(i);
})
OS Memory
OS Memory method can check total memory and free memory. Memory stands for Random access memory (RAM). The units for memory is in bytes.
3095019520
const os=require('os');
console.log(os.totalmem());
console.log(os.freemem());
Total Memory
Total Memory is the actual size of RAM. To get Total, memory, use os.totalmem() method. The default unit is in bytes. To get RAM size in GB, divide it by 1073741824, i.e. (1024*1024*1024). To get RAM size in MB, divide it by 1048576, i.e (1024*1024).
Total RAM in GBs
const os=require('os');
console.log((os.totalmem()/1073741824)+ "GB" );
Total RAM in MBs
const os=require('os');
console.log((os.totalmem()/1048576)+ "MB");
Free Memory
os.freemem method is used to check free memory out of total memory.
Check free memory
const os=require('os');
console.log(os.freemem());
os.networkInterfaces
os.networkInterfaces method returns an object containing information related to network interfaces assigned to the device. That includes mac id, ip address etc.
{
lo: [
{
address: '127.0.0.1',
netmask: '255.0.0.0',
family: 'IPv4',
mac: '00:00:00:00:00:00',
internal: true,
cidr: '127.0.0.1/8'
},
{
address: '::1',
netmask: 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff',
family: 'IPv6',
mac: '00:00:00:00:00:00',
scopeid: 0,
internal: true,
cidr: '::1/128'
}
],
eth0: [
{
address: '192.168.1.108',
netmask: '255.255.255.0',
family: 'IPv4',
mac: '01:02:03:0a:0b:0c',
internal: false,
cidr: '192.168.1.108/24'
},
{
address: 'fe80::a00:27ff:fe4e:66a1',
netmask: 'ffff:ffff:ffff:ffff::',
family: 'IPv6',
mac: '01:02:03:0a:0b:0c',
scopeid: 1,
internal: false,
cidr: 'fe80::a00:27ff:fe4e:66a1/64'
}
]
}
const os=require('os');
console.log(os.networkInterfaces());
OS Platform
os.platform method returns a string containing platform of operating system. That can be "darwin", "win32", "linux" etc.
const os=require('os');
console.log(os.platform());
OS Type
os.type method returns a string containing name of operating system. That can be "Darwin", "Windows_NT", "Linux" etc.
const os=require('os');
console.log(os.type());
OS uptime
os.uptime method returns system uptime in seconds.
6168.466 mins
102.807 hours
const os=require('os');
console.log(os.uptime()+" secs");
console.log((os.uptime()/60)+ " mins");
console.log((os.uptime()/3600)+ " hours");
OS userInfo
os.userInfo method returns an object with information of user like, uid, gid, username, home directory and default shell.
{
uid: 501,
gid: 20,
username: 'avinashmalhotra',
homedir: '/Users/avinashmalhotra',
shell: '/bin/zsh'
}
const os=require('os');
console.log(os.userInfo())