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

Large Language Models (LLMs): Four Critical Modeling Stages

Large language models (LLMs) have fundamentally transformed our digital landscape, powering everything from chatbots and…

4 days ago

Agentic Workflow Design Patterns Explained with Examples

As Large Language Models (LLMs) evolve into autonomous agents, understanding agentic workflow design patterns has…

5 days ago

What is Data Strategy?

In today's data-driven business landscape, organizations are constantly seeking ways to harness the power of…

6 days ago

Mathematics Topics for Machine Learning Beginners

In this blog, you would get to know the essential mathematical topics you need to…

1 month ago

Questions to Ask When Thinking Like a Product Leader

This blog represents a list of questions you can ask when thinking like a product…

1 month ago

Three Approaches to Creating AI Agents: Code Examples

AI agents are autonomous systems combining three core components: a reasoning engine (powered by LLM),…

1 month ago