Expose Functionality from a Node file using exports

Module.exports API to expose Data to other files

Node supports built-in module system. Node.js can import functionality which are exposed by other Node.js files. To import something, you need to use the import functionality exposed by the library.js file that is present in the current file folder.

const library = require('./library')                // Path

The functionality in the file must be exposed before it could be imported into any other files. An object defined in the file by default is private and unexposed to the outer world.

The module.exports file API is offered by the module system to implement in the code.
module is a variable that represents the current module and exports is an object which will be exposed as a module. So, the module.exports and exports will both be exposed as a module.

module.exports is basically an object which returns the result of a require call.

You need the new exports property to import the object or function in any other parts of your app. You can do so in 2 ways:

The first way is to assign an object to module.exports where the object is provided out of the box by module system.

Examples:

filter_none

edit
close

play_arrow

link
brightness_4
code

const person = {
    firstName: 'John',
    lastName: 'Smith'
}
  
module.exports = Person
  
// in the file where you want to export
   
const person= require(‘./person)

chevron_right


The second way is by adding the exported object as a property of exports. You can use exports to export multiple objects, function or data:

filter_none

edit
close

play_arrow

link
brightness_4
code

const Person = {
    firstName: 'John',
    lastName: 'Smith'
}
  
  
exports.person = person

chevron_right


Or Directly

filter_none

edit
close

play_arrow

link
brightness_4
code

exports.person = {
    firstName: 'John',
    lastName: 'Smith'
}

chevron_right


You’ll use it by referencing a property of your import in the other file:

filter_none

edit
close

play_arrow

link
brightness_4
code

Const items = require('items')
items.person

chevron_right


Or

filter_none

edit
close

play_arrow

link
brightness_4
code

const person= require('./items').person

chevron_right


What’s the difference between module.exports and exports?

The first exposes the object it points to whereas the latter exposes the properties of the object it points to.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.