Categories: Javascript

Javascript – Module, Module.Exports & related Best Practices

This article represents definition and code samples on how to modularize Javascript functions and use them elsewhere in different Javascript file. Please feel free to comment/suggest if I missed to mention one or more important points. Also, sorry for the typos.

Following are the key points described later in this article:

  • What is Module?
  • What is Module.exports?
  • Test the Module

 

  • Module: A unit encapsulating similar functions or a piece of code representing similar functions. For example, take a look at following code:
    var drawTraingle = function() {
     console.log("Traingle drawn");
    }
    
    var drawCircle = function() {
     console.log("Circle drawn");
    }
    

    Above code could be saved as draw.js. The file draw.js could be used to represent a module representing functions to draw different shapes. As per this page, module.exports is the object that’s actually returned as the result of a require call.

  • Module.exports: Once the code representing similar function is written, the next requirement is to use that code elsewhere in different Javascript files. This is achieved using module.exports. Module.exports is a NodeJS construct which wraps the similar function and makes it available in other JS files. With module.exports, one could only choose to share specific functions with the application. In case, you are not going to use NodeJS module, it is recommended to use just “exports” instead of “module.exports” as shown in one of the examples below. Take a look at diagram below for better understanding of module.exports, exports, and require:
    module.exports = {
        drawTraingle: function() {
            console.log("Traingle drawn");
        },
    
        drawCircle: function() {
            console.log("Circle drawn");
        }
    };
    

    The above code could also be written as following:

    var drawTraingle = function() {
        console.log("Traingle drawn");
    };
    
    var drawCircle = function() {
        console.log("Circle drawn");
    };
    
    exports.drawTraingle = drawTraingle;
    exports.drawCircle = drawCircle;
    

    Following code represents how the above function can be used in other file, say, main.js

    var d = require("./draw.js");
    
    d.drawTraingle();
    d.drawCircle();
    

    Pay attention to usage of “require” function.

  • Test the module: Place the above two files namely draw.js and main.js in a folder. Execute the main.js using node runtime such as following:
    node main.js
    

    It would print following:

    Traingle drawn
    Circle drawn
    
Ajitesh Kumar

I have been recently working in the area of Data analytics including Data Science and Machine Learning / Deep Learning. I am also passionate about different technologies including programming languages such as Java/JEE, Javascript, Python, R, Julia, etc, and technologies such as Blockchain, mobile computing, cloud-native technologies, application security, cloud computing platforms, big data, etc. I would love to connect with you on Linkedin. Check out my latest book titled as First Principles Thinking: Building winning products using first principles thinking.

Share
Published by
Ajitesh Kumar

Recent Posts

How Indexing Works in LLM-Based RAG Applications

When building a Retrieval-Augmented Generation (RAG) application powered by Large Language Models (LLMs), which combine…

1 day ago

Retrieval Augmented Generation (RAG) & LLM: Examples

Last updated: 25th Jan, 2025 Have you ever wondered how to seamlessly integrate the vast…

2 days ago

What are AI Agents? How do they work?

Artificial Intelligence (AI) agents have started becoming an integral part of our lives. Imagine asking…

3 weeks ago

Agentic AI Design Patterns Examples

In the ever-evolving landscape of agentic AI workflows and applications, understanding and leveraging design patterns…

3 weeks ago

List of Agentic AI Resources, Papers, Courses

In this blog, I aim to provide a comprehensive list of valuable resources for learning…

3 weeks ago

Understanding FAR, FRR, and EER in Auth Systems

Have you ever wondered how systems determine whether to grant or deny access, and how…

3 weeks ago