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 Real-Time Retrospective Board With Video Chat
An Absolute Beginner Guide to Node Package Manager
How To Build A Skin For Your Web App With React And WordPress
Speakeasy Two-factor Authentication in Node.js
Node.js Callback Concept
Node.js vs. PHP – Which is better for Backend development?
Creating A Continuous Integration Test Workflow Using GitHub Actions
Working with Moment.js Date Libraries
React Server Side Rendering With Node And Express
Linting in Node.js using ESLint
Building A Node.js Express API To Convert Markdown To HTML
Building A Node.js Application Using Docker
Writing A Multiplayer Text Adventure Engine In Node.js: Creating The Terminal Client (Part 3)
Web Scraping With Node.js
Understanding HTTP Requests in Node.js
Building a RESTful Web API in Node.js using PostgresSQL and Express
Consuming the TinEye Reverse Image Search API in Node.js
Exploring Node.js Internals
Using Prisma with Postgres and Node.js
A Vanilla Node.js REST API without Frameworks such us Express
Basics of SSH and Building an Application in Node.js
React To The Future With Isomorphic Apps
Node.js applications following an MVC architecture
How to Create a Simple REST API using TypeScript and Node.js
Getting Started with Node.js Event Emitter
How To Secure Your Web App With HTTP Headers
Why Node.js is Great for Backend Development?
Implementing a GraphQL server using Prisma, SQLite, and Nest.js with Typescript
Converting A Static Site to A Dynamic Node.js Web App
Building A Room Detector For IoT Devices On Mac OS
Optimizing Critical-Path Performance With Express Server And Handlebars
How to build a real time chat application in Node.js