Introduction to Modules in Nodejs along with their 3 Types

modules in nodejs

Modules in Nodejs are the blocks of encapsulated code that communicates with an external application on the basis of their related functionality. There are 3 types of Modules in Nodejs.

Introduction

What is Module ?

In Node.js, Modules are the blocks of encapsulated code that communicates with an external application on the basis of their related functionality. Modules can be a single file or a collection of multiple files / folder. The reason programmer are heavily reliant on the modules is because of their re-usability as well as the ability to break down a complex piece of code into manageable chunks.


Types of Modules

There are three types of modules in Node.js. They are as follows:
  • Core Modules
  • Local  Modules
  • Third-Party Modules


1. Core Modules

Node.js has many built-in modules that are part of the platform and comes with Node.js installation. These modules can be loaded into the program by using the require function.

Syntax:

const module = require("module_name");

The require() function will return a JavaScript type depending on what the particular module returns.

The following list consists some of the important core modules in Node.js:
  • http - Creates a HTTP server in Node.js.
  • assert - Set of assertion function useful for testing.
  • fs - Used to handle file system.
  • path - Includes methods to deal with file paths.
  • process - Provides information and control about the current Node.js process.
  • os - Provides information about the operating system.
  • querystring - Utility used for parsing and formatting URL query strings.
  • url - Module provides utilities for URL  resolution and parsing.
Learn more about built-in modules of Node.js.


2. Local  Modules

Unlike built-in modules and external modules, local modules are created locally in your Node.js application.

Example:

//myModule.js
exports.sum = function (a, b){
    return a + b;
}

Lets import myModule from index.js

//index.js
const myModule = require("myModule.js");
console.log(myModules.sum(5,10));

Now run index.js file.

> node index.js
15 //output


3. Third-party Modules

Third-party modules are modules that are available online using the Node Package Manager (NPM). These modules can be installed in the project folder or globally. Some of the popular third-party modules are mongoose, express, angular and react.

Example:
npm install express

npm install mongooose

Conclusion

So, this is a basic introduction on Modules in Nodejs along with their types. Visit the official Page of Node.js to know more about Node.js modules.

To get more tips, ticks and guides on Coding follow The Info Docx.
REFERENCE- Poor Coders


Also Read:

TAGS-  modules in nodejs, modules in node js, creating node js modules, node js built in modules, how to use es6 modules in node, built-in modules in node js, third party modules in node js, module.exports node js, how node.js modules are available externally, node.js require, nodejs import, fs module in node js, node js documentation

Post a Comment

Previous Post Next Post