Signup/Sign In

Node.js Modules - Built-in and Local Modules

Posted in Programming   LAST UPDATED: JANUARY 6, 2020

    While writing Node.js applications you can write the whole code in the file and when you run the application it works fine (provided there are no errors). The Node.js interpreter doesn't actually care about how you write the code or how complex your code is. But as the application grows bigger it might become difficult for someone to understand or debug the code.

    One technique to avoid this can be to organize/structure your code so that it is easier to understand or debug the code. Just like when you keep things organized you don't find it hard to find them, similarly organizing the code is a common practice in software development. Structuring the code makes it easier for others to understand or debug your code easily. This is where the word module comes into the picture.

    Modules are a way to keep the code structured so that it is easily understandable and is more scalable. So you can think of Node.js modules as JavaScript libraries - a certain part of the codebase which you want to keep together, but at the same time keep the things separated to make things clean.

    The module.exports or exports is a special object which is included in every JS file in the Node.js application by default. The module is a variable that represents the current module and exports is an object that will be exposed as a module. So, whatever you assign to module.exports or exports, will be exposed as a module.

    For example,

    module.exports = 'Hi there!!';

    or simply,

    exports = 'Hi there!!';



    Node.js Module Types

    There are two types of Node.js modules: Built-in Modules and Local Modules. Let's cover them both one by one.


    1. Built-in Node.js Modules

    Node.js provides us with already built-in modules in Node.js environment. We can use the built-in modules in our code by using the require function. These modules of Node.js also provide us with functions that can be used by us.

    Some of the examples of built-in modules are:

    Modules

    Description

    http

    The http module includes classes, methods, and events to create Node.js HTTP server.

    url

    This module includes methods for URL resolution and parsing.

    querystring

    The querystring module includes methods to deal with the query string.

    path

    This module includes methods to deal with file paths.

    util

    This module includes utility functions useful for programmers.


    To use the built-in modules, you need to import it using the require() function. For example,

    var module = require('module_name')

    The require function will return an object, function, property depending on what the module returns.

    For example, let's use the http module,

    var http = require('http');
    
    // code to create server
    var server = http.createServer(function(req,res) {
        // your code code
    });
    
    server.listen(3000);

    You can use a dot(.) to use the methods of the module eg: http.createServer().


    2. Local Node.js Modules

    Local modules are the modules that are created locally in your Node.js application. Local modules are created by a user for the purpose of reusing the module. These modules include different functionalities depending on your application.

    For example, creating a module named great.js which will have a function greatest() which can be used to find out the greatest number out of the 3 given numbers.

    function greatest(a,b,c){
        if(a>=b && a>=c)
            return a;
        else if(b>=a && b>=c)
            return b;
        else
            return c;
    }
    
    // here exports is an object to which you can attach properties or methods.
    exports.numberGreatest = function(){
        return greatest(10,30,80);
    }

    The great.js file provides the attribute to the outer world via exports. To use the great.js file's code inside any other module, we use the following code,

    var greatestNumber = require('./great.js');
    console.log(greatestNumber.numberGreatest());

    In the above example, the require() function will return an object which will contain a maximum of a, b, c and will assign it to the greatestNumber variable.

    When you will run the above code using the command node app.js, you will get the following output,

    Output:

    80

    By hiding implementation details in a module and only exposing those parts which are to be used by other parts of your codebase, you can keep your codebase well organized. Hence, using modules in your node.js application can prove to be very helpful if you are planning to build a scalable and large application.

    About the author:
    Working as an Application Developer. I love to learn and discover things JavaScript, HTML, and CSS.
    Tags:NodeJSJavaScriptNodeJS Modules
    IF YOU LIKE IT, THEN SHARE IT
     

    RELATED POSTS