Home Node.js Node.js OS Module

Node.js OS Module

OS is a node module which is used to provide information about the computer operating system.

Table of Contents

Syntax

The syntax for including the OS module in your application:

var os = require('os');

Let's see a list of generally used functions or methods.

Method Description
arch() This method returns the operating system CPU architecture
constants This method returns an object containing the operating system's constants for process signals, error cotes etc.
cpus() This method returns an array containing information about the computer's CPUs
endianness() This method returns the endianness of the CPU
EOL This method returns the end-of-line marker for the current operating system
freemem() This method returns the number of free memory of the system
hostname() This method returns the hostname of the operating system
loadavg() This method returns an array containing the load averages, (1, 5, and 15 minutes)
networkInterfaces() This method returns the network interfaces that has a network address
platform() This method returns information about the operating system's platform
release() This method returns information about the operating system's release
tmpdir() This method returns the operating system's default directory for temporary files
totalmem() This method returns the number of total memory of the system
type() This method returns the name of the operating system
uptime() This method returns the uptime of the operating system, in seconds
userInfo() This method returns information about the current user

Example

Lets see an example of some commonly used methods

const os=require('os'); 
 
console.log("os.freemem(): \n",os.freemem());  
console.log("os.homedir(): \n",os.homedir());  
console.log("os.hostname(): \n",os.hostname());  
console.log("os.endianness(): \n",os.endianness());  
console.log("os.loadavg(): \n",os.loadavg());  
console.log("os.platform(): \n",os.platform());  
console.log("os.release(): \n",os.release());  
console.log("os.tmpdir(): \n",os.tmpdir());  
console.log("os.totalmem(): \n",os.totalmem());  
console.log("os.type(): \n",os.type());  
console.log("os.uptime(): \n",os.uptime());

This displayed the following

C:\Users\user\nodejs>node osexample.js
os.freemem():
13673635840
os.homedir():
C:\Users\user
os.hostname():
DESKTOP-UGL2N6Q
os.endianness():
LE
os.loadavg():
[ 0, 0, 0 ]
os.platform():
win32
os.release():
10.0.19042
os.tmpdir():
C:\Users\user\AppData\Local\Temp
os.totalmem():
34308112384
os.type():
Windows_NT
os.uptime():
2231280

You may also like