• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

Node.js : Operating System Utilities in Node.js OS Module

August 25, 2015 //  by Mohamed Sanaulla//  Leave a Comment

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 lang=”javascript”] var os = require(‘os’);
[/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 :

  1. os.tmpdir()
  2. os.endianness()
  3. os.hostname()
  4. os.type()
  5. os.platform()
  6. os.arch()
  7. os.release()
  8. os.uptime()
  9. os.loadavg()
  10. os.totalmem()
  11. os.freemem()
  12. os.cpus()
  13. os.networkInterfaces()
  14. 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 lang=”javascript”] console.log("OS Temp Dir: " + os.tmpdir());
[/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 lang=”javascript”] console.log("EOL Marker for OS: " + os.EOL);
[/code]

The complete program is given below:

[code lang=”javascript”] //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);
[/code]

The above can be executed by running the command: node node_os_interface.js. The output is given below:

[code lang=”shell”] 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:

[/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.

Category: NodeJSTag: NodeJS Tutorials

About Mohamed Sanaulla

In his day job he works on developing enterprise applications using ADF. He is also the moderator of JavaRanch forums and an avid blogger.

Previous Post: «Spring Boot Configurations Working with SQL Databases and Spring Boot
Next Post: Complete Guide for Spring Boot Actuator Spring Boot Configurations»

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

np.zeros

A Complete Guide To NumPy Functions in Python For Beginners

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact