Top 5 Most Useful Node.js Built-in Modules

Top 5 Most Useful Node.js Built-in Modules

In the Previous article Understand-Nodejs-Modules, we covered some basics about the Node.js module system and created a custom module for demo purposes, in this post we’ll take look at some most useful built-in node.js modules.

so, let’s get started…

How to use Node.js Built-in Modules:

1. Path Module

Path Module provides utilities for working with file and directory paths. for example get a directory of the path, get file extension of the path, check the absolute path and so on…

Sample code:

const path = require("path");
var pathObj = path.parse(__filename);
console.log(pathObj);
//It returns follwing output in terminal
{
  root: 'D:\\', 
  dir: 'D:\\Nodejs\\hello_world', 
  base: 'app.js', ext: '.js',
  name: 'app' 
}

2. OS Module

we can use os Module to get information about the current operating system, with using OS modules we can get information like a free memory of machine , total memory of machine, get information about a current user, uptime of machine and so on…

 Sample Code:

const os = require("os");
var totalMemory = os.totalmem();
var freeMemory = os.freemem();

console.log("Total Memory :" + totalMemory);//it returns 'Total Memory :4194279424'
console.log("Free Memory :" + freeMemory);//it returns 'Free Memory :670097408'

3.Files system Module

The Node.js file system module allows to work with the file system on your computer, for example, read files, create files, update files, remove files, get a list of files from a directory and so on…

A Node.js file system modules provide two to access the same functinality with asynchronous and synchronous method for example to readdir you have two method readdir and another one readdirSync, so both will provide the same result but for real-world application you must have to use asynchronous method because as node.js is single threaded so with that we’ll not make it busy while requesting another method.

 Sample Code:

const fs = require("fs");
fs.readdir("./", function(err, files) {
  if (err) console.log("Error", err);
  else console.log("Files :", files);
});

//It return follwing output in terminal
Files : [ 'app.js', 'node_modules', 'package-lock.json', 'package.json' ]

4.HTTP Module

HTTP module we use for creating networking application, for example, we can create a web server that listens for HTTP requests on given port so with this we can easily crate API services for our application frontend. like a web application that we build with Agular, React or mobile application on a mobile device.

Sample Code:

const http = require("http");
const server = http.createServer();
server.on("connection", socket => {
  console.log("New Connection...");
});
server.listen(3000);
console.log("Listening on port 3000...");

//It Returns 
Listening on port 3000... 
New Connection...

now go to your browser and type URL ‘http://localhost:3000’ and you will get ‘New Connection’ string on your terminal every time you refresh browser you’ll get a new string.

5.URL Module

The URL module splits up a web address into readable parts.

Sample Code:

const url = require("url");

const urlAddress = "http://www.demoUrl.com/index.html?username=alexa";

var result = url.parse(urlAddress, true);

console.log(result.host); //returns 'www.demourl.com'
console.log(result.pathname); //returns '/index.html'
console.log(result.search); //returns '?username=alexa'

 so, that’s it we have covered some useful build modules of node.js there are also a bunch of other useful modules exist which you can find on node.js official website nodejs.org.

Also read,

Conclusion:

Thanks for reading. in this post, we have covered some basic introduction of node js module system.

 Do let me know If you face any difficulties please feel free to comment below we love to help you. if you have any feedback suggestions then please inform us by commenting.

Subscribe to our Newsletter

Stay up to date! Get all the latest posts delivered straight to your inbox.

If You Appreciate What We Do Here On TutsCoder, You Should Consider:

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Support Us

We are thankful for your never ending support.

Leave a Comment