Easy Tutorial
❮ Nodejs Npm Nodejs Dns Module ❯

Node.js OS Module

Node.js Utility Module

The Node.js os module provides some basic operating system functions. We can include this module by using the following method:

var os = require("os")

Methods

No. Method & Description
1 os.tmpdir() <br>Returns the operating system's default directory for temporary files.
2 os.endianness() <br>Returns the CPU byte order, which could be "BE" or "LE".
3 os.hostname() <br>Returns the hostname of the operating system.
4 os.type() <br>Returns the operating system name.
5 os.platform() <br>Returns the operating system name at compile time.
6 os.arch() <br>Returns the operating system CPU architecture, possible values are "x64", "arm", and "ia32".
7 os.release() <br>Returns the operating system's release version.
8 os.uptime() <br>Returns the operating system uptime in seconds.
9 os.loadavg() <br>Returns an array containing the 1, 5, and 15-minute average load.
10 os.totalmem() <br>Returns the total amount of system memory in bytes.
11 os.freemem() <br>Returns the amount of free system memory in bytes.
12 os.cpus() <br>Returns an array of objects containing information about each installed CPU/core: model, speed (in MHz), and times (an object containing the milliseconds the CPU/core spent in user, nice, sys, idle, and irq).
13 os.networkInterfaces() <br>Gets the list of network interfaces.

Properties

No. Property & Description
1 os.EOL <br>A constant defining the operating system's end-of-line marker.

Example

Create a main.js file with the following code:

var os = require("os");

// CPU byte order
console.log('endianness : ' + os.endianness());

// Operating system name
console.log('type : ' + os.type());

// Operating system platform
console.log('platform : ' + os.platform());

// Total system memory
console.log('total memory : ' + os.totalmem() + " bytes.");

// Free system memory
console.log('free memory : ' + os.freemem() + " bytes.");

The output of the code execution is as follows:

$ node main.js 
endianness : LE
type : Linux
platform : linux
total memory : 25103400960 bytes.
free memory : 20676710400 bytes.

Node.js Utility Module

❮ Nodejs Npm Nodejs Dns Module ❯