Last updated Feb 25, 2021

Top 5 Node.js Built-in Modules Every Developer Should Know

Discover the most useful Node.js core modules and how they simplify backend development. A quick guide for building faster, scalable apps.

Top 5 Node.js Built-in Modules Every Developer Should Know

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.

Tags

Available for projectsLet’s build together

Need this built properly,
not just explained?

I take on Angular and NestJS work directly — auth systems, multi-tenant SaaS, API rewrites and the deploy pipeline underneath them. You talk to the person writing the code.

Projects delivered
20+ Projects delivered
Angular • NestJS
7+ years Angular • NestJS
Average reply time
1 day Average reply time

What I can help with

  • Auth and permissions systems
  • Multi-tenant SaaS foundations
  • API design and rewrites
  • Deploy pipelines and VPS setup
  • Bug fixing and performance optimization
Send me a message

Tell me about your project and I’ll get back with a realistic scope and price.

Turn
Your Ideas
Into Reality

Stay in the loop

Get the finished tutorials, projects and resources in your inbox.

Join 5,000+ developers and get new tutorials, source code, product updates and exclusive content — straight to your inbox.

  • No spam
  • Useful content only
  • Unsubscribe anytime

Subscribe to TutsCoder

Be the first to get new tutorials, projects and tips.

Join 5,000+ developers already learning with us.

Learn
Build
Grow

Discussion

Comments

Share your thoughts, ask questions or suggest improvements. Let’s learn and grow together!

Loading comments…

Join the discussion

Sign in to join the discussion. Your account also keeps your saved tutorials and path progress.