Course contentsShow
← JavaScript
Lesson 1441 of 2,72331. ModulesFree lesson

Exporting Objects and Functions

Export multiple items by assigning an object to module.exports with named properties.

Exporting Objects and Functions

You'll learn how to bundle and export multiple items from a single module using an object assigned to module.exports.

The Problem with Single Exports

In the previous lesson, you learned that module.exports can hold one value—a string, number, function, or object. But what if you want to share multiple functions or values from the same file? You could create separate files for each, but that's inefficient.

The Solution: Export an Object

The elegant solution is to assign an object to module.exports, where each property represents something you want to share:

// mathUtils.js
function add(a, b) {
  return a + b;
}

function subtract(a, b) {
  return a - b;
}

module.exports = {
  add: add,
  subtract: subtract
};

Now other files can require this module and access both functions:

// app.js
const math = require('./mathUtils.js');

console.log(math.add(5, 3));      // 8
console.log(math.subtract(10, 4)); // 6

Think of It Like a Toolbox

Imagine module.exports as a toolbox you're handing to someone. Instead of giving them a single screwdriver, you're giving them a box containing a screwdriver, hammer, and wrench (your functions). They receive the whole box and can pick which tool they need.

Mix and Match

Your exported object can contain functions, values, or even nested objects:

module.exports = {
  PI: 3.14159,
  add: add,
  subtract: subtract
};

Key Takeaway: Export multiple functions and values by assigning an object with named properties to module.exports, creating a single importable collection of related utilities.