Getting Started with Node.js Module

We use the Node.js Operating System (OS) module to get more information about the underlying computer system. In this tutorial, you will learn the basic operating system module operations in Node.js.

1. Getting started

JavaScript has improved since the release of ES2015, employing the use of modules. This feature allows the reusability of scripts across multiple files.

Given this advantage, Node.js is no exception, it organizes most of its core functionalities in modules.

This tutorial assumes you’ve basic knowledge in Node.js and its core features.

Operating System (in Node.js) is a module, and therefore we’ll need to import it into our script before accessing its core methods.

Let’s start by creating a file node-os-module.js and add the following line.

const os = require('os');

Now that we’ve got the OS module, in the next section, let’s have a look at various operations we can perform with the OS module.

2. Getting operating system details

This module has a couple of functions that are used to retrieve the details of an operating system as we’ll see shortly.

In the node-os-module.js file, add the following the scripts:

const os = require('os');

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

console.log(myCurrentOSDetails);

Run this script in your terminal, by typing the following in your shell:

node node-os-module.js 

Running this script in Ubuntu 20.04 outputs the following (note that results vary depending on your system):

{
  name: 'Linux',
  architecture: 'x64',
  platform: 'linux',
  release: '5.8.0-45-generic',
  version: '#51~20.04.1-Ubuntu SMP Tue Feb 23 13:46:31 UTC 2021'
}

You can achieve the same result by using the Node.js’ Read, Evaluate, Print and Loop (REPL) command-line tool.

Start by running the following command in your terminal to launch REPL :

$ node

Upon running the node command, you will notice that you’re presented with a screen with the > symbol, this is the REPL command line (as shown below).

$ node
Welcome to Node.js v15.12.0.
Type ".help" for more information.
> 

Now let’s proceed and write our previous script in the REPL as shown below:

$ node
Welcome to Node.js v15.12.0.
Type ".help" for more information. 
> const os = require('os');
undefined
> 
> let myCurrentOSDetails = {
...     name: os.type(),
...     architecture: os.arch(),
...     platform: os.platform(),
...     release: os.release(),
...     version: os.version()
... };
undefined
>

Since our script is assigned to myCurrentOSDetails variable, we can log our result as shown below:

> console.log( myCurrentOSDetails );
> {
  name: 'Linux',
  architecture: 'x64',
  platform: 'linux',
  release: '5.8.0-45-generic',
  version: '#51~20.04.1-Ubuntu SMP Tue Feb 23 13:46:31 UTC 2021'
}
undefined
>

3. Checking computer system (server) uptime

We’ve seen how we can use this module to get the server’s underlying operating system details, in this section, let’s look at the server uptime functionality.

Uptime in computers refers to the availability of the system to perform its operations, unlike downtime where the system is stalled/shut down and unable to perform its task.

This module has the os.uptime() method, which returns the system uptime details in seconds (from the time your system was last powered on).

Let’s look at an example:

In your REPL window, enter the following command:

...
> console.log(`Server running for the past ${os.uptime()} seconds.`);

Output:

...
> console.log(`Server running for the past ${os.uptime()} seconds.`);
Server running for the past 750588.69 seconds.
undefined
> 

4. Getting user information (current system user)

The OS module has the os.userinfo() method that returns the details of the current system user (remember the computer system may have a root user and a normal user depending on its configurations).

You can achieve this task by running the following REPL command (remember to import the os module as discussed previously):

console.log(os.userInfo());

Output in Ubuntu 20.04:

> console.log(os.userInfo());
{
  uid: 1000,
  gid: 1000,
  username: 'xxxmixxxer',
  homedir: '/home/xxxmixxxer',
  shell: '/bin/bash'
}
undefined
>

5. Computer system hardware information

OS module has several methods to get detailed information about a computer system, with our focus mainly on hardware.

They include:

  • os.totalmem() – This method returns the computer’s primary storage (memory) in bytes.

We can achieve this by typing the following in our REPL:

 > let memory = os.totalmem();
undefined
> 
 > console.log(memory);
16628015104
undefined
> 

NOTE: The value returned is in bytes, which might require additional arithmetics for conversions. i.e 16628015104/1024 = ~16GB of RAM.

  • os.freemem() – This method returns the computer’s free space. The size of space available in bytes.

For example:

 > let freeMememorySpace = os.freemem();
undefined
> 
 > console.log(freeMememorySpace);
1298567168
undefined
> 

6. Conclusion

We’ve seen how we can use the os module to get more information about a computer system. This is important especially when a piece of knowledge about the hosting (maybe a server) platform is required.

Various operations are available such as getting details about the computer’s network interface which you can read from here.

Happy learning!

Related posts:

How to Integrate B2C M-Pesa API using Node.js
Getting Started with Node.js 15
Uploading Files Using Multer in a Node.js Application
Creating A Continuous Integration Test Workflow Using GitHub Actions
Concepts of TCP, Explained with Node.js
Node.js Callback Concept
How to Build a Static site with Gatsby.js
The Issue With Global Node Packages
Generating Authentication Token for Agora Applications
Node.js Network Requests using Axios
The Guide To Ethical Scraping Of Dynamic Websites With Node.js And Puppeteer
How to Build a Custom URL Shortener using Node.js, Express, and MongoDB
Create and Deploy NPM Packages
A Deep Dive Into Eleventy Static Site Generator
Introduction to Job Scheduling in Node.js
How To Build A Node.js API For Ethereum Blockchain
How to Build an Authentication API with JWT Token in Node.js
Top Node.js Interview Questions
Building A Node.js Application Using Docker
Build and Dockerize a Full-stack React app with Node.js, MySQL and Nginx
Why is Node.js wildly popular among developers?
Why Node.js is Good for Online Stores
Getting Started with Google Translate API with Node.js
Creating Secure Password Flows With NodeJS And MySQL
How to Generate Fake Data in Node.js Using Faker.js
Getting Started with EJS Templating Engine
Agora Cloud Recording with Node.js
Open-source packages & Code Security using NPM
Writing A Multiplayer Text Adventure Engine In Node.js (Part 1)
How to build a real time chat application in Node.js
Optimizing Critical-Path Performance With Express Server And Handlebars
React Server Side Rendering With Node And Express