Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære 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

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 1. Kapitel 3

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

Awesome!

Completion rate improved to 7.14

bookExporting with module.exports

Stryg for at vise menuen

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

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 1. Kapitel 3
some-alt