See how Frete cut frontend build time by 70%

What are best AI tools? Take the State of AI survey

Builder.io
Builder.io
Contact sales

See how Frete cut frontend build time by 70%

What are best AI tools? Take the State of AI survey

Builder.io
Builder.io
< Back to blog

Web Development

Node.js module.exports vs. exports: The Right Choice

February 28, 2023

Written By Vishwas Gopinath

If you've worked with Node.js, you're probably familiar with module.exports and exports keywords used to export code from one module to another. While they may seem interchangeable at first glance, there's a good reason to use one over the other.

Import export patterns

Let's start by going over a few CommonJS import export patterns in Node.js.

Modifying module.exports properties

In the first pattern, functions are attached to properties on the module.exports object. We can see the correct values logged in index.js.

Modifying exports properties

In the second pattern, you attach functions to properties on the exports object. The correct values are still logged in index.js.

Now, this raises the question: why use module.exports when exports seems to achieve the same result with fewer keystrokes? To answer that, let's take a look at the following two patterns.

Assigning new object to module.exports

In the third pattern, a new object is assigned to module.exports. The correct values are then logged in index.js.

Assigning new object to exports

In the fourth pattern, a new object is assigned to exports. However, this pattern does not seem to work, as math appears to be an empty object. Let's understand why.

Object references in JavaScript

Let's revisit how object references work in JavaScript. When you assign one object to another, both objects point to the same memory address. Modifying one will modify the other as well. Let's see this in action.

In this example, both superhero1 and superhero2 reference the same superhero. Modifying superhero2 also modifies superhero1.

However, assigning a new object will break the reference.

In this case, the assignment breaks the reference and modifying superhero2 no longer affects superHero1.

module.exports vs. exports

Now that we understand how objects work in JavaScript, let's relate it to module.exports and exports. In Node.js, module is a plain JavaScript object with an exports property. exports is a plain JavaScript variable that happens to be set to module.exports. When you require a module in another file, the code within that module is executed, and only module.exports is returned.

However, if you assign a new object to exports, the reference is broken and updating exports no longer updates module.exports.

If you try to access add or subtract on the exported object, an error is thrown because module.exports is empty. So, while module.exports and exports may seem interchangeable in the first import export pattern, they are not the same.

Conclusion

When should you choose exports over module.exports? The short answer is that you probably shouldn't. While exports may be shorter and seem more convenient, the confusion it can cause is not worth it. Remember that exports is just a reference to module.exports, and assigning a new object to exports breaks that reference.

Share

Twitter
LinkedIn
Facebook

Announcing Builder 2.0:

Multiplayer coding

Real-time collaboration, parallel agents, and visual editing. The whole team ships real code with Al now.


Continue Reading
AI9 MIN
The Future of SaaS Is Cloneable
WRITTEN BY Alice Moore
May 6, 2026
AI8 MIN
6 Best GitHub Copilot Alternatives in 2026
WRITTEN BY Vishwas Gopinath
May 5, 2026
AI3 MIN
Announcing Quality Review Agent: Agentic QA on Every PR
WRITTEN BY Vishwas Gopinath
April 30, 2026

Product

Visual CMS

Theme Studio for Shopify

Sign up

Login

Featured Integrations

React

Angular

Next.js

Gatsby

Resources

User Guides

Developer Docs

Forum

Blog

Github

Get In Touch

Chat With Us

Twitter

Linkedin

Careers

© 2020 Builder.io, Inc.

Security

Privacy Policy

Terms of Service

Get the latest from Builder.io

By submitting, you agree to our Privacy Policy

  • Fusion

  • Publish

  • Product Updates

  • Design to Code

  • Headless CMS

    Multi-Brand CMS

  • Landing Pages

  • Web Apps

  • Prototypes

  • Marketing Sites

  • Headless Commerce

Developer Resources

  • Documentation

  • Fusion Docs

  • Publish Docs

  • Figma AI to Production Code

  • AI Prototyping for Product Managers

  • Figma to Storybook

  • Figma to App Converter

  • All Resources

  • Blog

  • Webinars

  • Guides

  • Customer Stories

  • Community Forum

  • Partners

  • Affiliate Program

  • CMS Integrations

  • CMS Blueprints

  • Explainers

  • Glossary

  • Agent-Native

  • Figma to Code Guide

  • Headless CMS Guide

  • Headless Commerce Guide

  • Composable DXP Guide

  • About

  • Careers

  • Contact Sales

Security

Privacy Policy

SaaS Terms

Trust Center

Cookie Preferences

YouTube icon
Github icon
Blsky Icon
Twitter "X" icon
LinkedIn icon
Feed Icon