This tutorial walks through the usage of Operating System related utilities in the Node.js OS module. The OS module in Node.js offers wide set of methods that are useful in getting the relevant details about the native operating system.
If you are Node.js beginner, please read our Introduction article on Node.js.
Node.js provides operating system related utilities in os module. In this article we will show you the different APIs provided by the os module. We will show each API and its output along with it. Let us first create a js file by name: node_os_interface.js
and add the following line to it:
[/code]
Node.js OS Module API
There are 14 methods defined in the OS module of Node.js. These are the methods that are used for reading and interacting with native Operating System to fetch various details like process, memory, etc. The list of methods that are part of the OS module are :
- os.tmpdir()
- os.endianness()
- os.hostname()
- os.type()
- os.platform()
- os.arch()
- os.release()
- os.uptime()
- os.loadavg()
- os.totalmem()
- os.freemem()
- os.cpus()
- os.networkInterfaces()
- os.EOL
The following sections explains each method in the Node.js OS module with simple examples.
tmpdir()
Now let us start with the first API tmpdir()
– this API prints the temporary directory of the OS:
[/code]
The above prints: OS Temp Dir: C:\Users\Mohamed\AppData\Local\Temp
on my system. It will be different for different systems.
endianness()
This API prints whether the CPU architecture is Big Endian (BE) or Little Endian (LE).
[code lang=”javascript”] console.log("CPU is BigEndian(BE) or LittleEndian(LE): " + os.endianness());[/code]
Output
[code lang=”shell”] CPU is BigEndian(BE) or LittleEndian(LE): LE[/code]
hostname()
This API prints the operating system hostname.
[code lang=”javascript”] console.log("OS Hostname: " + os.hostname());[/code]
Output
[code lang=”shell”] OS Hostname: Sana-Laptop[/code]
type()
This API prints the type of the operating system.
[code lang=”javascript”] console.log("OS Type: " + os.type());[/code]
Output
[code lang=”shell”] OS Type: Windows_NT[/code]
platform()
This API prints the platform of the OS.
[code lang=”javascript”] console.log("OS Platform: " + os.platform());[/code]
Output
[code lang=”shell”] OS Platform: win32[/code]
arch()
This API prints CPU architecture – whether it is 32 bit, 64 bit or arm architecture.
[code lang=”javascript”] console.log("OS CPU Architecture: " + os.arch());[/code]
Output
[code lang=”shell”] OS CPU Architecture: x64[/code]
release()
This API prints OS release number.
[code lang=”javascript”] console.log("OS Release: " + os.release());[/code]
Output
[code lang=”shell”] OS Release: 6.3.9600[/code]
uptime()
This API returns the uptime of the machine i.e the number of seconds it has been running.
[code lang=”javascript”] console.log("OS Uptime (seconds): " + os.uptime());[/code]
Output
[code lang=”shell”] OS Uptime (seconds): 104535.1365887[/code]
loadavg()
This API returns the load average of the machine in the last 1, 5 and 15 minutes. This concept is relevant to UNIX systems and will return 0,0,0 for windows systems.
[code lang=”javascript”] console.log("OS load average (Returns 0,0,0 in windows): " + os.loadavg());[/code]
Output
[code lang=”shell”] OS load average (Returns 0,0,0 in windows): 0,0,0[/code]
totalmem()
This API returns the total memory (RAM) available in the system.
[code lang=”javascript”] console.log("Total RAM (mb): " + (os.totalmem()/1024)/1024);[/code]
Output
[code lang=”shell”] Total RAM (mb): 8084.2734375[/code]
freemem()
This API returns the free memory (RAM) available in the system.
[code lang=”javascript”] console.log("Free RAM (mb): " + (os.freemem()/1024)/1024)[/code]
Output
[code lang=”shell”] Free RAM (mb): 2169.56640625[/code]
cpus()
This API returns the CPUs available and information about them. We will use JSON.stringify function to pretty print the JSON string.
[code lang=”javascript”] var cpus = os.cpus();console.log("CPU Information: " + JSON.stringify(cpus, null, 2));
[/code]
Output
[code lang=”shell”] CPU Information:[
{
"model": "Intel(R) Core(TM) i7-4510U CPU @ 2.00GHz",
"speed": 2594,
"times": {
"user": 2376265,
"nice": 0,
"sys": 2880406,
"idle": 66987203,
"irq": 239203
}
},
{
"model": "Intel(R) Core(TM) i7-4510U CPU @ 2.00GHz",
"speed": 2594,
"times": {
"user": 2378703,
"nice": 0,
"sys": 2435625,
"idle": 67429250,
"irq": 136937
}
},
{
"model": "Intel(R) Core(TM) i7-4510U CPU @ 2.00GHz",
"speed": 2594,
"times": {
"user": 2435640,
"nice": 0,
"sys": 2670859,
"idle": 67137046,
"irq": 25000
}
},
{
"model": "Intel(R) Core(TM) i7-4510U CPU @ 2.00GHz",
"speed": 2594,
"times": {
"user": 2503703,
"nice": 0,
"sys": 1726234,
"idle": 68013625,
"irq": 24640
}
}
] [/code]
The above information contains the CPU speed and the time the CPU has spent in doing user operations, system operations and being idle for each CPU.
networkInterfaces()
This API returns the network interfaces in the system i.e the entities that interface between OS and the network. These can be physical devices and logical entities for connection to localhost.
[code lang=”javascript”] console.log("Network Interfaces: " + JSON.stringify(os.networkInterfaces(), null, 2));[/code]
Output
[code lang=”shell”] Network Interfaces:{
"Wi-Fi": [
{
"address": "fe80::19c3:55f9:ecd:8a5a",
"netmask": "ffff:ffff:ffff:ffff::",
"family": "IPv6",
"mac": "b0:10:41:68:31:53",
"scopeid": 2,
"internal": false
},
{
"address": "192.168.1.4",
"netmask": "255.255.255.0",
"family": "IPv4",
"mac": "b0:10:41:68:31:53",
"internal": false
}
],
"Loopback Pseudo-Interface 1": [
{
"address": "::1",
"netmask": "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff",
"family": "IPv6",
"mac": "00:00:00:00:00:00",
"scopeid": 0,
"internal": true
},
{
"address": "127.0.0.1",
"netmask": "255.0.0.0",
"family": "IPv4",
"mac": "00:00:00:00:00:00",
"internal": true
}
],
"Teredo Tunneling Pseudo-Interface": [
{
"address": "2001:0:9d38:6abd:2444:2b76:8a3f:ec06",
"netmask": "ffff:ffff:ffff:ffff::",
"family": "IPv6",
"mac": "00:00:00:00:00:00",
"scopeid": 0,
"internal": false
},
{
"address": "fe80::2444:2b76:8a3f:ec06",
"netmask": "ffff:ffff:ffff:ffff::",
"family": "IPv6",
"mac": "00:00:00:00:00:00",
"scopeid": 8,
"internal": false
}
] }
[/code]
Each of the above network interfaces have an entry each for IPv4 and IPv6.
EOL
This returns the EOL marker for the OS. In windows it is \n
.
[/code]
The complete program is given below:
[code lang=”javascript”] //file name: node_os_interface.jsvar os = require(‘os’);
console.log("OS Temp Dir: " + os.tmpdir());
console.log("CPU is BigEndian(BE) or LittleEndian(LE): " + os.endianness());
console.log("OS Hostname: " + os.hostname());
console.log("OS Type: " + os.type());
console.log("OS Platform: " + os.platform());
console.log("OS CPU Architecture: " + os.arch());
console.log("OS Release: " + os.release());
console.log("OS Uptime (seconds): " + os.uptime());
console.log("OS load average (Returns 0,0,0 in windows): " + os.loadavg());
console.log("Total RAM (mb): " + (os.totalmem()/1024)/1024);
console.log("Free RAM (mb): " + (os.freemem()/1024)/1024)
var cpus = os.cpus();
console.log("CPU Information: " + JSON.stringify(cpus, null, 2));
console.log("Network Interfaces: " + JSON.stringify(os.networkInterfaces(), null, 2));
console.log("EOL Marker for OS: " + os.EOL);
[/code]
The above can be executed by running the command: node node_os_interface.js
. The output is given below:
CPU is BigEndian(BE) or LittleEndian(LE): LE
OS Hostname: Sana-Laptop
OS Type: Windows_NT
OS Platform: win32
OS CPU Architecture: x64
OS Release: 6.3.9600
OS Uptime (seconds): 145774.5905403
OS load average (Returns 0,0,0 in windows): 0,0,0
Total RAM (mb): 8084.2734375
Free RAM (mb): 2064.69921875
CPU Information:
… CPU Information already shown above …
Network Interfaces:
… Network interface information already shown above …
EOL Marker for OS: [/code]
I hope this tutorial helped you to understand the Node.js OS module with simple examples. In my next articles, I will cover few more topics on Node.js framework with more examples.