Monday, January 18, 2021

The Three ways of exporting in Node JS

First approach is like this. I.e instead of using module.exports , just use exports.var1 = var1

 

// film_school.js


// a beginner film course

let film101 = {

    professor: 'Mr Caruthers',

    numberOfStudents: 20,

    level: 'easy'

}

 

// an expert film course

let film102 = {

    professor: 'Mrs Duguid',

    numberOfStudents: 8,

    level: 'challenging' 

}

 

// export the courses so other modules can use them

exports.film101 = film101;

exports.film102 = film102;



Other long hand approach was 

// export the courses so other modules can use them

module.exports.film101 = film101;

module.exports.film102 = film102;


Now we can use Also like the below 


module.exports = {

    film101: film101,

    film102: film102

}


References:

https://stackabuse.com/how-to-use-module-exports-in-node-js/

No comments:

Post a Comment