# ES Modules: import & export

> Break your codebase into manageable files. Learn the syntax for named and default exports to share code across your application.

Subject: JavaScript | Lesson 46 of 63 | 5 min read
Interactive lesson: https://codewithanimation.in/javascript/modules-esm
Previous: [Forms & User Input](https://codewithanimation.in/javascript/forms-and-input.md) | Next: [Optional Chaining & Nullish Coalescing](https://codewithanimation.in/javascript/optional-chaining-nullish.md)

## The Module System

Historically, all JavaScript was dumped into a single massive file or loaded via multiple `<script>` tags, causing global variables to collide and overwrite each other.

**ES Modules (ESM)** is the modern standard for organizing JavaScript. It allows you to break your code into separate files (modules) that have their own private scope. You explicitly declare what to share (`export`) and what to bring in (`import`).

## Named vs Default

There are two ways to export data from a module:

1. **Named Exports:** Use when exporting multiple things. You must use the exact same name when importing, wrapped in curly braces: `import { sum, multiply } from './math.js';`

2. **Default Exports:** Use when a file exports exactly one primary thing (like a single React component). You can name it whatever you want when importing, without curly braces: `import MathLibrary from './math.js';`

## Writing Modules

To create a named export, put the keyword in front of the declaration:

```javascript
export const PI = 3.14;
export function add(a, b) { return a + b; }
```

To create a default export, use `export default`:

```javascript
export default function calculateArea(r) { return PI * r * r; }
```

## Common mistake: The Node.js Split

Before ESM existed, Node.js invented its own module system called **CommonJS** (`require()` and `module.exports`).

Today, JavaScript is split between these two systems. While ESM (`import`/`export`) is the modern standard for browsers and new frameworks, you will still encounter CommonJS in older server-side codebases. They are not natively compatible without transpilation or special configuration.

## Tip: Aliasing Imports

If two different modules export a function with the same name, you can rename one of them upon import using the `as` keyword: `import { processData as processUserData } from './user.js';`

## Try it: Visualizing the Wiring

Study the architecture diagram below. It illustrates the exact syntax required to wire an exported variable from one file into an imported variable in another.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/javascript/modules-esm

## Check yourself

### Question 1

Which syntax correctly imports a Named Export from 'utils.js'?

- A. import { validateUser } from './utils.js';
- B. import validateUser from './utils.js';
- C. import * as validateUser from './utils.js';

Correct answer: A

Named imports strictly require curly braces `{}` and must exactly match the exported name.

- Why not B: Incorrect. This syntax is for Default exports, not Named exports.
- Why not C: Incorrect. This imports everything in the file into a single object.

### Question 2

Can a single JavaScript file have multiple Default exports?

- A. No, a file can only have one Default export.
- B. Yes, you can have as many Default exports as you want.
- C. Yes, but they must have different names.

Correct answer: A

A file can have infinite Named exports, but strictly only one Default export.

- Why not B: Incorrect. The browser wouldn't know which one to pick when you use `import Foo from '...'`.
- Why not C: Incorrect. Default exports are anonymous to the importing file.

### Question 3

What was the main problem with loading multiple `<script>` tags before ES Modules existed?

- A. All variables shared the same global scope, causing naming collisions and overriding.
- B. The browser couldn't run multiple script tags at the same time.
- C. It required an active internet connection to download.

Correct answer: A

Without modules, a variable named `config` in script A would completely overwrite a variable named `config` in script B if they were both loaded on the same page.

- Why not B: Incorrect. It could run them, but order mattered heavily.
- Why not C: Incorrect. That is true of any external file, not specific to the script tag system.

## Remember this

- ES Modules (ESM) give every file its own private scope.
- Use **Named Exports** (`export const x`) for multiple items, and import them with curly braces (`import { x }`).
- Use **Default Exports** (`export default x`) for a single main item, and import without curly braces.
- Named imports must use the exact name, while Default imports can be named anything by the importer.
- Use the `as` keyword to rename a Named import to prevent naming collisions.
