Sign up: Designing Enterprise UI Directly on Production Code

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

Builder.io
Builder.io
Contact sales

Sign up: Designing Enterprise UI Directly on Production Code

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

Generate high quality code that uses your components & design tokens.

Try it nowGet a demo

Continue Reading
AI8 MIN
Subagents: When and How to Use Them
WRITTEN BY Alice Moore
February 3, 2026
AI9 MIN
Best LLMs for coding in 2026
WRITTEN BY Matt Abrams
January 28, 2026
AI8 MIN
Lovable Alternatives for 2026
WRITTEN BY Alice Moore
January 27, 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

  • Documentation

  • Fusion Docs

  • Publish Docs

  • Figma AI to Production Code

  • AI Prototyping for Product Managers

  • Figma to Storybook

  • Figma to App Converter

  • Blog

  • Webinars

  • Guides

  • Case Studies

  • Community Forum

  • Partners

  • Affiliate Program

  • CMS Integrations

  • CMS Blueprints

  • Glossary

  • Figma to Code Guide

  • Headless CMS Guide

  • Headless Commerce Guide

  • Composable DXP Guide

  • About

  • News

  • 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