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

Retrieval Augmented Generation (RAG) & LLM: Examples

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

2 months ago

How to Setup MEAN App with LangChain.js

Hey there! As I venture into building agentic MEAN apps with LangChain.js, I wanted to…

2 months ago

Build AI Chatbots for SAAS Using LLMs, RAG, Multi-Agent Frameworks

Software-as-a-Service (SaaS) providers have long relied on traditional chatbot solutions like AWS Lex and Google…

2 months ago

Creating a RAG Application Using LangGraph: Example Code

Retrieval-Augmented Generation (RAG) is an innovative generative AI method that combines retrieval-based search with large…

2 months ago

Building a RAG Application with LangChain: Example Code

The combination of Retrieval-Augmented Generation (RAG) and powerful language models enables the development of sophisticated…

2 months ago

Building an OpenAI Chatbot with LangChain

Have you ever wondered how to use OpenAI APIs to create custom chatbots? With advancements…

2 months ago