Expose functionality from a Node file using exports
How to use the module.exports API to expose data to other files in your application, or to other applications as well
Node has a built-in module system.
A Node.js file can import functionality exposed by other Node.js files.
When you want to import something you use
const library = require('./library')
to import the functionality exposed in the library.js file that resides in the current file folder.
In this file, functionality must be exposed before it can be imported by other files.
Any other object or variable defined in the file by default is private and not exposed to the outer world.
This is what the module.exports API offered by the module system allows us to do.
When you assign an object or a function as a new exports property, that is the thing that’s being exposed, and as such, it can be imported in other parts of your app, or in other apps as well.
You can do so in 2 ways.
The first is to assign an object to module.exports, which is an object provided out of the box by the module system, and this will make your file export just that object:
const car = {
brand: 'Ford',
model: 'Fiesta'
}
module.exports = car
//..in the other file
const car = require('./car')
The second way is to add the exported object as a property of exports. This way allows you to export multiple objects, functions or data:
const car = {
brand: 'Ford',
model: 'Fiesta'
}
exports.car = car
or directly
exports.car = {
brand: 'Ford',
model: 'Fiesta'
}
And in the other file, you’ll use it by referencing a property of your import:
const items = require('./items')
items.car
or
const car = require('./items').car
What’s the difference between module.exports and exports?
The first exposes the object it points to. The latter exposes the properties of the object it points to.
download all my books for free
- javascript handbook
- typescript handbook
- css handbook
- node.js handbook
- astro handbook
- html handbook
- next.js pages router handbook
- alpine.js handbook
- htmx handbook
- react handbook
- sql handbook
- git cheat sheet
- laravel handbook
- express handbook
- swift handbook
- go handbook
- php handbook
- python handbook
- cli handbook
- c handbook
subscribe to my newsletter to get them
Terms: by subscribing to the newsletter you agree the following terms and conditions and privacy policy. The aim of the newsletter is to keep you up to date about new tutorials, new book releases or courses organized by Flavio. If you wish to unsubscribe from the newsletter, you can click the unsubscribe link that's present at the bottom of each email, anytime. I will not communicate/spread/publish or otherwise give away your address. Your email address is the only personal information collected, and it's only collected for the primary purpose of keeping you informed through the newsletter. It's stored in a secure server based in the EU. You can contact Flavio by emailing [email protected]. These terms and conditions are governed by the laws in force in Italy and you unconditionally submit to the jurisdiction of the courts of Italy.