module.exports in Javascript – Node JS, Express

Module.exports is used with creating new module in javascript. Certain functionality of the app can be moved to a separate module. And it can be exported using this exports, then it can be easily imported to any page of the app and used.

module.exports in JS

module.exports is a javascript object created by the Module system. Lets check an example of exporting a function using module.exports.

eg:

date.js

module.exports = getDate;

function getDate(){
    let today = new Date();

    let options = {
        weekday: "long",
        day: "numeric",
        month: "long"
    };
    return today.toLocaleDateString("en-US", options);
}

app.js

//

const date = require(__dirname + "/date.js");

let day = date();

console.log(day);

About the Author: smartcoder

You might like

Leave a Reply

Your email address will not be published. Required fields are marked *