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:
var os = require('os');
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:
console.log("OS Temp Dir: " + os.tmpdir());
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).
console.log("CPU is BigEndian(BE) or LittleEndian(LE): " + os.endianness());
Output
CPU is BigEndian(BE) or LittleEndian(LE): LE
hostname()
This API prints the operating system hostname.
console.log("OS Hostname: " + os.hostname());
Output
OS Hostname: Sana-Laptop
type()
This API prints the type of the operating system.
console.log("OS Type: " + os.type());
Output
OS Type: Windows_NT
platform()
This API prints the platform of the OS.
console.log("OS Platform: " + os.platform());
Output
OS Platform: win32
arch()
This API prints CPU architecture – whether it is 32 bit, 64 bit or arm architecture.
console.log("OS CPU Architecture: " + os.arch());
Output
OS CPU Architecture: x64
release()
This API prints OS release number.
console.log("OS Release: " + os.release());
Output
OS Release: 6.3.9600
uptime()
This API returns the uptime of the machine i.e the number of seconds it has been running.
console.log("OS Uptime (seconds): " + os.uptime());
Output
OS Uptime (seconds): 104535.1365887
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.
console.log("OS load average (Returns 0,0,0 in windows): " + os.loadavg());
Output
OS load average (Returns 0,0,0 in windows): 0,0,0
totalmem()
This API returns the total memory (RAM) available in the system.
console.log("Total RAM (mb): " + (os.totalmem()/1024)/1024);
Output
Total RAM (mb): 8084.2734375
freemem()
This API returns the free memory (RAM) available in the system.
console.log("Free RAM (mb): " + (os.freemem()/1024)/1024)
Output
Free RAM (mb): 2169.56640625
cpus()
This API returns the CPUs available and information about them. We will use JSON.stringify function to pretty print the JSON string.
var cpus = os.cpus(); console.log("CPU Information: " + JSON.stringify(cpus, null, 2));
Output
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 } } ]
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.
console.log("Network Interfaces: " + JSON.stringify(os.networkInterfaces(), null, 2));
Output
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 } ] }
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
.
console.log("EOL Marker for OS: " + os.EOL);
The complete program is given below:
//file name: node_os_interface.js var 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);
The above can be executed by running the command: node node_os_interface.js
. The output is given below:
OS Temp Dir: C:\Users\Mohamed\AppData\Local\Temp 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:
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.