Node.JS OS Module

In the recent articles we studied node.js web module and then express framework. We also studied how express module is helpful in implementing the RESTful API. In this article we are going to briefly review Node.JS OS module. This module contains functions that are helpful in obtaining operating system related information. For instance, you can get OS name, OS architecture, the total amount of OS memory, the amount of free memory etc. Following are some of the most commonly used node.js OS module functions.

  • os.type(): It returns the name of the operating system.
  • os.platform(): Returns the platform of the operating system.
  • os.arch(): Returns the CPU architecture of the operating system for instance x86, x64 etc.
  • os.uptime(): Returns the total time since operating system has been up.
  • os.release(): Returns the release time of the operating system.
  • os.cpus(): Returns detailed information regarding all the cores installed on the system e.g speed,model and time.
  • os.totalmem(): Returns the total amount of memory of the system in bytes.
  • os.freemem(): Returns the total amount of free memory of the system in bytes

Node.JS OS Module Example

We have defined the usage of the most commonly used methods of OS module. Let’s see their working in action with the help of an example. Take a look at the following piece of code.

var os = require("os");




console.log('OS Type : ' + os.type());


console.log('OS platform : ' + os.platform());


console.log('OS Archi : ' + os.arch());


console.log('OS Uptime : ' + os.uptime() );


console.log('OS Release : ' + os.uptime() );

console.log('OS free memory : ' + os.freemem() + " bytes.");

console.log('OS Total memory : ' + os.totalmem() + " bytes.");

Download the code Try the code

The output of the above code depends upon the operating system on which the code is running. However, on my system the first function returns Windows_NT, the platform is Win32 and the architecture is X64. This means that I have a 64 bit windows operating system running on my machine.

OS module is extremely helpful in those cases where you program same application for multiple operating systems. You can place checks which implicate that if the underneath operating system is windows, do this else do that. In the next article, we will study path module in detail.

Copyright 2005-2016 KnowledgeHills. Privacy Policy. Contact .