Node Js Get Total and Available System Memory Tutorial

Last updated on: by Digamber

What if you need to check the system memory through Node js environment. If you belong to the node js domain, then this thing is not difficult.

In this quick tutorial, you will learn how to get the computer RAM in Node js using the built-in os module.

Node js is handy because it offers many modules that make it easy to use. In this guide, we will find out about the os module. This module provides an easy way to detect the total RAM of the system in Node js.

Not just that, it also helps you find out how to get the total available RAM or memory in Node js.

Let us find out how?

How to Find Total and Available Computer RAM (Memory) in Node Js

  • Step 1: Generate Project Folder
  • Step 2: Initialize NPM
  • Step 3: Create Main File
  • Step 4: Get System Memory
  • Step 5: Display Result on Console

Generate Project Folder

In the very first step, we will run the provided command and generate the project folder for node app.

mkdir node-tuts

Next, you have to enter into the project.

cd node-tuts

Initialize NPM

In the next step, you have to run the npm initializer command.

This command creates the package.json file that accumulates the project’s package, scripts, and essential metadata.

npm init

When running the above command, provide the project-related information on the console; this info will go directly into the package.json file.

Create Main File

In this step, you have to create a new file, give it a name like app.js.

Now, you need to register the file name into the scripts tag into the package.json file as given below.

...
...
...
  "scripts": {
    "start": "node app.js"
  },
...
...
...

Get System Memory

You are now ready to write some code to show system memory.

Make sure to declare the os module in the app.js file at the top most part.

There after, access the freemem() and totalmem methods through the os constructor.

const os = require('os')
const osFreeMem = os.freemem()
const allFreeMem = (osFreeMem / (1024 * 1024))
console.log(`Total free memory: ${allFreeMem}`)

const osTotalMem = os.totalmem()
const avbMem = (osTotalMem / (1024 * 1024))
console.log(`Total available RAM: ${avbMem}`)

Display Result on Console

In the final section, we show you how to print the system total memory and available memory through node js application.

Make sure to run the given command from the terminal.

node app.js

After you invoked the command following output display on your console.

Node Js Get Total and Available System Memory Tutorial

Conclusion

Throughout this post we have described how to obtain the system memory using a very simple os module of Node js.

The os module provides a simple freemem() and totalmem() methods that returns the total as well as the available system RAM in Node js. We hope this guide has helped you.

Digamber

I am Digamber, a full-stack developer and fitness aficionado. I created this site to bestow my coding experience with newbie programmers. I love to write on JavaScript, ECMAScript, React, Angular, Vue, Laravel.