Skip to content
Related Articles

Related Articles

How to Create Modules in Node.js ?
  • Last Updated : 11 May, 2020

Modules are the collection of JavaScript codes in a separate logical file that can be used in external applications on the basis of their related functionality. Modules are popular as they are easy to use and are reusable.

To create a module in Node.js, you will need the exports keyword. This keyword tells Node.js that the function can be used outside the module.

Syntax:

exports.function_name = function(arg1, arg2, ....argN) {
    // function body
};  

Example of a custom node module:

  • Create a file that you want to export




    // File name: calc.js
      
    exports.add = function (x, y) {
        return x + y;
    };
      
    exports.sub = function (x, y) {
        return x - y;
    };
      
    exports.mult = function (x, y) {
        return x * y;
    };
      
    exports.div = function (x, y) {
        return x / y;
    };
  • Use the ‘require’ keyword to import the file




    // File name: App.js
    var calculator = require('./calc');
      
    var x = 50, y = 20;
      
    console.log("Addition of 50 and 20 is "
                       + calculator.add(x, y));
      
    console.log("Subtraction of 50 and 20 is "
                       + calculator.sub(x, y));
      
    console.log("Multiplication of 50 and 20 is "
                       + calculator.mult(x, y));
      
    console.log("Division of 50 and 20 is " 
                       + calculator.div(x, y));
  • Output:
    Addition of 50 and 20 is 70
    Subtraction of 50 and 20 is 30
    Multiplication of 50 and 20 is 1000
    Division of 50 and 20 is 2.5
    



My Personal Notes arrow_drop_up
Recommended Articles
Page :