Node.js | Export Module

The module.exports in Node.js is used to export any literal, function or object as a module. It is used to include JavaScript file into node.js applications. The module is similar to variable that is used to represent the current module and exports is an object that is exposed as a module.

Syntax:

  • module.exports = literal | function | object

    Explanation: Here the assigned value (literal | function | object) is directly exposed as a module and can be used directly.

  • module.exports.variable = literal | function | object

    Explanation: Here the assigned value (literal | function | object) is indirectly exposed as a module and can be consumed using the variable.

Example 1: Exporting Literals



  • Create a file named as app.js and export the literal using module.exports.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    module.exports = "GeeksforGeeks";

    chevron_right

    
    

  • Create a file named as index.js and import the file app.js to print the exported literal to the console.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    const company = require("./app");
    console.log(company);

    chevron_right

    
    

  • Output:
    GeeksforGeeks

Example 2: Exporting Object

  • Create a file named as app.js and export the object using module.exports.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    module.exports = {
      name: 'GeeksforGeeks',
    }

    chevron_right

    
    

  • Create a file named as index.js and import the file app.js to print the exported object data to the console.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    const company = require('./app');
      
    console.log(company.name);
    console.log(company.website);

    chevron_right

    
    

  • Output:
    GeeksforGeeks
    https://geeksforgeeks.org
    

Example 3: Exporting Function

  • Create a file named as app.js and export the function using module.exports.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    module.exports = function (a, b) {
      console.log(a + b);
    }

    chevron_right

    
    

  • Create a file named as index.js and import the file app.js to use the exported function.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    const sum = require('./app');
      
    sum(2, 5);

    chevron_right

    
    

  • Output:
    7

Example 4: Exporting function as a class

  • Create a file named as app.js. Define a function using this keyword and export the function using module.exports.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    module.exports = function () {
      this.name = 'GeeksforGeeks';
      this.website = 'https://geeksforgeeks.org';
      this.info = () => {
        console.log(`Company name - ${this.name}`);
        console.log(`Website - ${this.website}`);
      }
    }

    chevron_right

    
    

  • Create a file named index.js and import the file app.js to use the exported function as a class.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    const Company = require('./app');
      
    const firstCompany = new Company();
      
    firstCompany.info();

    chevron_right

    
    

  • Output:
    Company name - GeeksforGeeks
    Website - https://geeksforgeeks.org
    

Example 5: Load Module from Separate Folders

filter_none

edit
close

play_arrow

link
brightness_4
code

const index = require('./models/lang/index.js');

chevron_right


Explanation: In the above example, the index.js file is under lang folder that is inside models folder. We can use the ./ to go to the root folder then we move relative to it.




My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.