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

The Exports Shorthand

Learn how exports is an alias for module.exports and when it breaks down.

The Exports Shorthand

You'll learn how exports provides a convenient alias for module.exports, and when that convenience breaks.

Understanding the Shortcut

In CommonJS, Node.js gives you two ways to export values: module.exports and exports. By default, exports is simply a reference pointing to the same object as module.exports. Think of them as two name tags attached to the same suitcase.

This means you can add properties to either one:

// Both do the same thing initially
exports.greet = function() { return "Hello"; };
module.exports.greet = function() { return "Hello"; };

When another file uses require(), they'll receive whatever module.exports points to—and since exports references that same object, adding properties works perfectly.

When the Alias Breaks

The shorthand breaks the moment you try to reassign exports itself:

// ❌ This DOESN'T work
exports = function() { return "Hello"; };

// ✅ This DOES work
module.exports = function() { return "Hello"; };

Why? When you write exports = ..., you're changing what the exports variable points to—but require() always returns module.exports, which remains unchanged. You've just moved one name tag to a different suitcase, but the system only looks at the suitcase labeled module.exports.

The Safe Pattern

Use exports.property when adding properties. Use module.exports when replacing the entire export with a single function, class, or value.

Key Takeaway: exports is a convenient alias for module.exports that only works for adding properties—direct reassignment breaks the link and won't affect what gets imported.