Node.js OS Module

Summary: in this tutorial, you will learn about the Node.js os module that allows you to perform operating system-related activity.

Introduction to the node.js OS module

To use the os module, you include it as follows:

const os = require('os');Code language: JavaScript (javascript)

The os module provides you with many useful properties and methods for interacting with the operating system and server.

For example, the os.EOL property returns the platform-specific end-of-line marker.

The os.EOL property returns \r\n on Windows and \n on Linux or macOS.

Getting the current Operating System information

The os module provides you with some useful methods to retrieve the operating system of the server. For example:

let currentOS = {
    name: os.type(),
    architecture: os.arch(),
    platform: os.platform(),
    release: os.release(),
    version: os.version()
};

console.log(currentOS);Code language: JavaScript (javascript)

Output:

{
    name: 'Windows_NT',
    architecture: 'x64',
    platform: 'win32',
    release: '10.0.18362',
    version: 'Windows 10 Pro'
}
Code language: JavaScript (javascript)

Checking server uptime

The os.uptime() method returns the system uptime in seconds. For example:

console.log(`The server has been up for ${os.uptime()} seconds.`);Code language: JavaScript (javascript)

Output:

The server has been up for 44203 seconds.Code language: JavaScript (javascript)

Getting the current user information

The os.userInfo() method returns the information about the current user:

console.log(os.userInfo());Code language: JavaScript (javascript)

Output:

{
    uid: -1,
    gid: -1,
    username: 'john',
    homedir: 'C:\\Users\\john',
    shell: null
}Code language: JavaScript (javascript)

Getting the server hardware information

The os.totalmem() method returns the total memory in bytes of the server:

let totalMem = os.totalmem();
console.log(totalMem);Code language: JavaScript (javascript)

Output:

8464977920Code language: JavaScript (javascript)

To get the amount of free memory in bytes, you use the os.freemem() method:

let freeMem = os.freemem();
console.log(freeMem);Code language: JavaScript (javascript)

Output:

1535258624Code language: JavaScript (javascript)

To get the information of the CPU, you use the os.cpus() method:

os.cpus();Code language: JavaScript (javascript)

The following example shows the model and speed of the server’s CPU:

const { model, speed } = os.cpus()[0];

console.log(`Model: ${model}`);
console.log(`Speed (MHz): ${speed}`);Code language: JavaScript (javascript)

Retrieving network interface information

The os.networkInterfaces() method returns an object that contains network interface information.

Each key in the returned object identifies a network interface:

console.log(os.networkInterfaces());Code language: JavaScript (javascript)

Output:

os.networkInterfaces();Code language: JavaScript (javascript)

In this tutorial, you have learned some properties and methods of the Node.js os module to interact with the operating system.

Was this tutorial helpful ?