Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Exporting with module.exports | Understanding and Using Node.js Modules
Working with Modules and Packages in Node.js

bookExporting with module.exports

When you want to share functions, objects, or values from one file so that another file can use them, you use module.exports in Node.js. This is the foundation of how modules communicate. You define your functions or objects in one file, then assign them to module.exports. Any file that uses require to load this module will receive whatever you assigned to module.exports.

mathUtils.js

mathUtils.js

copy

Start by creating a function or object you want to export. In the example above, the add function and the calculator object are defined in mathUtils.js. By assigning an object containing both add and calculator to module.exports, you make both available to other files.

When another file, such as app.js, uses require('./mathUtils'), Node.js loads the module and returns the value of module.exports. This means mathUtils in app.js is the object you exported, so you can call mathUtils.add(5, 3) or mathUtils.calculator.subtract(10, 4).

You can export a single value, like a function, or an object containing multiple values. If you assign a new value to module.exports, that is what will be returned by require. Always remember: only what you attach to module.exports will be accessible outside the module.

Note
Note

In Node.js, exports is just a shortcut reference to module.exports. You can use exports.greet = ... to add properties, but if you reassign module.exports directly, the shortcut no longer applies.

// Valid: adding a property
exports.greet = () => "Hello!";

// Invalid: reassigning exports breaks the link
exports = () => "Hello!"; // Won't work
question mark

How do you export a function or object from a Node.js module so it can be used in another file?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 1. ChapterΒ 3

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

Can you show an example of how to use `module.exports` and `require` in two files?

What happens if I assign multiple values to `module.exports` separately?

Can you explain the difference between `exports` and `module.exports`?

Awesome!

Completion rate improved to 7.14

bookExporting with module.exports

Swipe to show menu

When you want to share functions, objects, or values from one file so that another file can use them, you use module.exports in Node.js. This is the foundation of how modules communicate. You define your functions or objects in one file, then assign them to module.exports. Any file that uses require to load this module will receive whatever you assigned to module.exports.

mathUtils.js

mathUtils.js

copy

Start by creating a function or object you want to export. In the example above, the add function and the calculator object are defined in mathUtils.js. By assigning an object containing both add and calculator to module.exports, you make both available to other files.

When another file, such as app.js, uses require('./mathUtils'), Node.js loads the module and returns the value of module.exports. This means mathUtils in app.js is the object you exported, so you can call mathUtils.add(5, 3) or mathUtils.calculator.subtract(10, 4).

You can export a single value, like a function, or an object containing multiple values. If you assign a new value to module.exports, that is what will be returned by require. Always remember: only what you attach to module.exports will be accessible outside the module.

Note
Note

In Node.js, exports is just a shortcut reference to module.exports. You can use exports.greet = ... to add properties, but if you reassign module.exports directly, the shortcut no longer applies.

// Valid: adding a property
exports.greet = () => "Hello!";

// Invalid: reassigning exports breaks the link
exports = () => "Hello!"; // Won't work
question mark

How do you export a function or object from a Node.js module so it can be used in another file?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 1. ChapterΒ 3
some-alt