OS Module in NodeJS

Prathamesh More
Analytics Vidhya
Published in
3 min readApr 1, 2020

OS module provides information related to the operating system and hardware.

The os module provides API for getting information about hardware related like CPU, memory, directories, IP address and many more.

In this tutorial, we will learn basics methods and some more concepts of os module.

To start working with the os module, we have to import osmodule in our project.

const os = require('os);

os.arch()

This method will return the architecture of the processor.

const os = require('os');console.log(os.arch());

Output:

x64

os.cpus()

This method returns an array of the object which contains information of logical CPUs.

const os = require('os);console.log(os.cpus());

Output:

[
{
model: 'Intel(R) Core(TM) i5-7300HQ CPU @ 2.50GHz',
speed: 2496,
times: {
user: 6088781,
nice: 0,
sys: 6719718,
idle: 44304250,
irq: 1336078
}
},
{
model: 'Intel(R) Core(TM) i5-7300HQ CPU @ 2.50GHz',
speed: 2496,
times: {
user: 6955656,
nice: 0,
sys: 5061265,
idle: 45095515,
irq: 169078
}
},
{
model: 'Intel(R) Core(TM) i5-7300HQ CPU @ 2.50GHz',
speed: 2496,
times: {
user: 6605562,
nice: 0,
sys: 4668015,
idle: 45838859,
irq: 95781
}
},
{
model: 'Intel(R) Core(TM) i5-7300HQ CPU @ 2.50GHz',
speed: 2496,
times: {
user: 7106781,
nice: 0,
sys: 4634140,
idle: 45371515,
irq: 68109
}
}
]

os.freemem()

This method returns free main memory bytes in integer.

const os = require('os');console.log(os.freemem());

Output:

2954100736

os.getPriority(pid)

This method returns the scheduling priority of the process.

We have to pass pid as an argument.

const os = require('os');console.log(os.getPriority(13512));

Here is my case, I provided pid of Windows's File Explorer .

Output:

0

os.homedir()

This method current user’s home directory as a string.

const os = require('os');console.log(os.homedir());

Output:

C:\Users\Prathamesh More

os.hostname()

This method returns the hostname of the operating system i.e. the computer name as a string.

const os = require('os');console.log(os.hostname());

Output:

Prathamesh-Omen

os.networkInterfaces()

This method returns objects containing information about network interfacing devices.

const os = require('os');console.log(os.networkInterfaces());

Output:

{
WiFi: [
{
address: 'fe80::bc77:2fb3:a3f:1e22',
netmask: 'ffff:ffff:ffff:ffff::',
family: 'IPv6',
mac: '88:78:73:ef:c1:e9',
internal: false,
cidr: 'fe80::bc77:2fb3:a3f:1e22/64',
scopeid: 6
},
{
address: '192.168.43.61',
netmask: '255.255.255.0',
family: 'IPv4',
mac: '88:78:73:ef:c1:e9',
internal: false,
cidr: '192.168.43.61/24'
}
],
'Local Area Connection* 2': [
{
address: 'fe80::c0fc:bfd3:8f90:7a1c',
netmask: 'ffff:ffff:ffff:ffff::',
family: 'IPv6',
mac: '8a:78:73:ef:c1:e9',
internal: false,
cidr: 'fe80::c0fc:bfd3:8f90:7a1c/64',
scopeid: 15
},
{
address: '192.168.137.1',
netmask: '255.255.255.0',
family: 'IPv4',
mac: '8a:78:73:ef:c1:e9',
internal: false,
cidr: '192.168.137.1/24'
}
],
'Loopback Pseudo-Interface 1': [
{
address: '::1',
netmask: 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff',
family: 'IPv6',
mac: '00:00:00:00:00:00',
internal: true,
cidr: '::1/128',
scopeid: 0
},
{
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'
}
]
}

os.platform()

This method return information about platform i.e. operation system platform like win64 arm linux etc.

const os = require('os');console.log(os.platform());

Output:

win32

os.totalmem()

This method returns total system memory in bytes as a string.

const os = require('os');console.log(os.totalmem());

I have 8 Gigs.

Output:

8469581824

os.userInfo([options])

This method returns the current user. The returned object includes the username, uid, gid, shell, and homedir. On Windows, the uid and gid fields are -1, and shell is null.

options: If encoding is set to 'buffer', the username, shell, and homedir values will be Buffer instances. Default: 'utf8' .

const os = require('os');console.log(os.userInfo());

Output:

{
uid: -1,
gid: -1,
username: 'Prathamesh More',
homedir: 'C:\\Users\\Prathamesh More',
shell: null
}

In this tutorial, we covered most of the methods. You can explore more on the official documentation of NodeJS.

Thank you!

Happy coding!

Feel free to contact me!

pprathameshmore.github.io

--

--

Prathamesh More
Analytics Vidhya

Passionate, Self-taught Full Stack Web Developer that loves designing and building modern websites, applications, and APIs.