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:

Building a RESTful Web API in Node.js using PostgresSQL and Express
How To Auto-generate Admin Panels for Node.js with AdminBro
Agora Cloud Recording with Node.js
A Vanilla Node.js REST API without Frameworks such us Express
Writing A Multiplayer Text Adventure Engine In Node.js (Part 1)
How to build a GraphQL Server Using Node.js
Getting Started with Strapi API CMS
Session Management in Node.js using ExpressJS and Express Session
Python time Module
Exploring Node.js Internals
Linting in Node.js using ESLint
Most Useful Node.js Packages
Getting Started with Fastify Node.js Framework and Faunadb
Getting Started with Push Notifications in Node.js using Service Workers
How to Use Modular Patterns in Node.js
Getting Started with Json Web Auth using Angular 11 and Node.js
Why Node.js is Great for Backend Development?
Writing A Multiplayer Text Adventure Engine In Node.js: Adding Chat Into Our Game (Part 4)
Getting Started with the Quasar Framework
Useful Node.js Tools, Tutorials And Resources
Introduction to the Koa.js Framework
Consuming the Unsplash API using Node.js Graphql API
Email Authentication and Verification using Node.js and Firebase
Better Error Handling In NodeJS With Error Classes
The Nodemailer package in a Node.js Server
How to Consume a Co-operative Bank API using Node.js
Creating a Weather app in Node.js using the Openweathermap API
Why is Node.js wildly popular among developers?
Getting Started with Google Sheets API in Node.js
Node.js applications following an MVC architecture
How to Create a Simple REST API using TypeScript and Node.js
Getting Started with HTTP/2 in Node.js