DEV Community

Cover image for Part 1: Mastering Modules in Node.js
Ibrahim Ayandiran
Ibrahim Ayandiran

Posted on

Part 1: Mastering Modules in Node.js

In our digital world, everything around us is part of a vast tech ecosystem with software at its core. Over the years, a plethora of programming languages have been created to develop software. Some are problem-specific, some are multi-functional, and others have evolved from being problem-specific to multi-functional. JavaScript, at its genesis, was a programming language designed to bring interactivity to web applications. It's come a long way since then, and JavaScript now offers much more.

Node.js is a JavaScript runtime environment built on the Chrome V8 engine, allowing execution of JavaScript code outside web browsers. One of its core strengths lies in its modular structure. By the end of this article, you will have a clear understanding of what modules are, what problem they are trying to solve and how to use them.

Understanding Modules Through DRY and SRP

If you're familiar with the software development principles of DRY (Don't Repeat Yourself) and SRP (Single Responsibility Principle), then you've grasped the core problems that modules address. Modules are blocks of code that encapsulate related functionalities, making them reusable and maintainable. A well-written module adheres to both DRY and SRP principles, offering several advantages:

Code Reusability:

Instead of duplicating code across your application, you can create a module and reuse it wherever needed. This reduces redundancy and minimizes errors.

Maintenance:

Updating the logic becomes simpler and less error-prone. With modules, you only need to change the code in one place (the module itself) rather than modify it in multiple locations throughout your application.

Separation of Concerns:

Modules allow developers to break down complex applications into smaller, manageable parts with focused functionalities. This improves code readability and fosters better collaboration within development teams, as developers can work on specific modules without worrying about unintended side effects in other areas of the application.

Namespace Management:

Modules prevent naming conflicts. Variables and functions declared within a module are local to that module, avoiding clashes with global variables or variables used in other modules. to that module, avoiding clashes with global variables or variables used in other modules.

Types of Modules:

In Node.JS there are three(3) types of modules, these are;

Core/Built-in Modules:

These are built-in modules that come pre-installed with Node.js, providing essential functionalities like file system access (fs), HTTP communication (http), and path manipulation (path).

Local/Custom Modules:

These are modules you define within your application. They can be imported across different parts of your project extending its functionalities.

Third-Party Modules:

These are the third-party modules you import using the npm install or yarn add command. These modules are developed by other developers and published in the software registry.

How to create Local/Custom Modules:

Depending on your module syntax, creating a module in Node.js can be done either of the ways listed below:

CJS (Common JavaScript):
// Define code block 
function add(arg1, arg2) {
    return arg1 + arg2;
}

// Export code block
module.exports = add;
Enter fullscreen mode Exit fullscreen mode
// Define code block
class MathHelper {
    add(arg1, arg2) {
        return arg1 + arg2
    }
}
module.exports = MathHelper
Enter fullscreen mode Exit fullscreen mode
ES6 (ECMAScript 2015):
function add(arg1, arg2) {
    return arg1 + arg2;
}

// Export code block
export { add };
Enter fullscreen mode Exit fullscreen mode
export function add(arg1, arg2) {
    return arg1 + arg2;
}
`
Enter fullscreen mode Exit fullscreen mode
class MathHelper {
    add(arg1, arg2) {
        return arg1 + arg2
    }
}
// Export code block
export default add;
Enter fullscreen mode Exit fullscreen mode

Importing Modules in Node.js

Despite the three different types of modules in Node.js (core, local, and third-party), the import process remains consistent. However, the specific syntax you use might differ depending on your chosen module syntax style.

CJS (Common JavaScript):
// Built-in/Core Module
const http = require("http");

// Custom/Local Module 
const add = require("./mathUtils");

// Third-Party Module
const express = require("express")

Enter fullscreen mode Exit fullscreen mode
ES6 (ECMAScript 2015):
// Built-in/Core Module
import http from "http";

// Custom/Local Module 
import add from "./mathUtils";

// Third-Party Module
import express from "express"

Enter fullscreen mode Exit fullscreen mode

Conclusion

In this first part of our series on Node.js modules, we unpacked the essentials. We explored how modules promote code reusability, maintainability, and separation of concerns—the cornerstones of clean code. We also covered the different types of modules available: core modules, custom local modules you define within your project, and the vast ecosystem of third-party modules accessible through npm.

Stay tuned for part two, where we'll delve deeper into the technical aspects. We'll explore module wrappers, their role in providing private scope and caching, and how they differ from Immediately Invoked Function Expressions (IIFEs). We'll also discuss module caching in detail, explaining how Node.js optimizes performance by storing the results of required modules.
For further reading check out the Official Node.js Documentation on Modules

Top comments (0)