GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Home
  • ES6
  • Web
  • JS BOM
  • JS DOM
  • Web API
  • React
  • Node.js
  • Express
  • React Native
  • JavaScript Array Methods
  • JavaScript String Methods
  • Regular Expressions
  • ES Next

This view

  • Original page
  • New address

GeezerVison is showing www.javascripttutorial.net — Node.js OS Module

Home » Node.js Tutorial » 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?

Thank you for your feedback!

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.