DEV Community

Cover image for NodeJS Module System
Olasunkanmi Igbasan
Olasunkanmi Igbasan

Posted on

NodeJS Module System

A JavaScript run-time called Node.js is built on top of the Chrome v8 engine. In essence, the Node.js platform offers an environment for JavaScript code execution that is separate from the typical web browser (it's crucial to remember that Node.js was developed for the creation of network applications utilizing JavaScript).

The building blocks for building complex applications are modules in Node.
Using the module building system, you can segregate the codebase into manageable chunks that can be independently created and tested. By keeping all the functions and variables that are not explicitly designated to be exported private, modules are also the primary mechanism for enforcing information shielding.

Modules are of three types:

  • Core Modules

  • Local Modules

  • Third Party Modules

Core Modules: Node.js comes with a number of inbuilt modules that are installed as part of the infrastructure. The require function can be used to import these modules into the application.

const module = require('module-name')
Enter fullscreen mode Exit fullscreen mode

You need only refer to the module name when loading a core module or a module from the node modules subdirectory.

Local Modules:

A separate JavaScript file must be used to create the local module. We can declare a JavaScript object with various properties and functions in a separate file.

const Hello = {

    sayHello : function() {
        console.log("Hello  user");
  },

  curTime : new Date().toLocaleDateString(),


}

module.exports = Hello

Enter fullscreen mode Exit fullscreen mode

Using the above module;

const localModule = require("./Hello.js");
localModule.sayHello();
console.log(localModule.currTime);
Enter fullscreen mode Exit fullscreen mode

Third Party Modules :

Third party modules are often external modules installed through the Node Package Manager.

npm install express

Enter fullscreen mode Exit fullscreen mode

👆🏼 How to install third party modules.
👇🏼 How to import third party modules.

const express = require('express')
Enter fullscreen mode Exit fullscreen mode

How loading a module works

When loading a module, node first checks to see whether the module identifier (the string passed into the require function call) begins with "./," "/," or "../," and if it doesn't, node checks to see whether the identifier matches any of its core modules (http, path, fs, etc.). If a match is found, node loads the recognized core module; or else, node knows to browse the node modules folder.

The Need for modules.

  1. The ability to divide the codebase into several files
    This keeps the code more ordered, makes it simpler to understand, and helps maintain the independence of different functionality.

  2. Enabling code reuse across various projects: It's possible for a module to implement a generic feature that can be helpful for various projects.

  3. Encapsulation (information protection): Most module systems allow for the selective concealment of the private portion of the code while exposing the public interface, such as functions, classes, or objects, which are intended for usage by the module's consumers.

In conclusion, depending on the global scope is a very risky effort, especially as your application evolves. As a result, when a modification is required, just the module where the function is written is addressed.
When a change is required or when your application changes, the Module system prevents a complete breakdown.

The well-known DON'T REPEAT YOURSELF software development philosophy is taken to a whole new level with the Module system.

Top comments (3)

Collapse
 
iamjaydev profile image
iamjaydev

👏🏻👏🏻👏🏻

Collapse
 
tobisupreme profile image
Tobi Balogun

Thanks for putting some of the concepts I've been learning into words!

Collapse
 
olatisunkanmi profile image
Olasunkanmi Igbasan

💯💯