Node.js OS Module

Summary: in this tutorial, you will learn about the Node.js os module that allows you to perform the 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');

Or:

import os from 'os';

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:

import os from 'os';

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

console.log(currentOS);

Output:

{
    name: 'Windows_NT',
    architecture: 'x64',
    platform: 'win32',
    release: '10.0.18362',
    version: 'Windows 10 Pro'
}

Checking server uptime #

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

import os from 'os';

console.log(`The server has been up for ${os.uptime()} seconds.`);

Output:

The server has been up for 44203 seconds.

Getting the current user information #

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

import os from 'os';

console.log(os.userInfo());

Output:

{
    uid: -1,
    gid: -1,
    username: 'john',
    homedir: 'C:\\Users\\john',
    shell: null
}

Getting the server hardware information #

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

import os from 'os';

let totalMem = os.totalmem();
console.log(totalMem);

Output:

8464977920

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

import os from 'os';

let freeMem = os.freemem();
console.log(freeMem);

Output:

1535258624

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

os.cpus();

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}`);

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:

import os from 'os';

console.log(os.networkInterfaces());

Output:

{
  'Wi-Fi': [
    {
      address: '192.168.102.3',
      netmask: '255.255.255.0',
      family: 'IPv4',
      mac: '4a:cc:93:5d:f3:19',
      internal: false,
      cidr: '192.168.102.3/24'
    }
  ],
  'Loopback Pseudo-Interface 1': [
    {
      address: '::1',
      netmask: 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff',
      family: 'IPv6',
      mac: '00:00:00:00:00:00',
      internal: true,
      cidr: '::1/128',
      scopeid: 0
    },
    {
      address: '127.0.0.1',
      netmask: '255.0.0.0',
      family: 'IPv4',
      mac: '00:00:00:00:00:00',
      internal: true,
      cidr: '127.0.0.1/8'
    }
  ]
}

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

Was this helpful?