Why Modules Matter: Building for Scale
The `import`/`export` syntax (ES6 Modules) isn't just a "nice-to-have" feature; it is the **fundamental architecture** of all modern JavaScript applications. Every major framework (React, Vue, Svelte, Angular) and every modern tool (Vite, Webpack) is built upon it. Understanding modules is understanding how to build scalable, maintainable software.
1. The Dependency Tree & Tree Shaking
When you `import` one file into another, you create a connection. When you do this hundreds of times in an application, you create a "dependency tree". Your `index.js` is the trunk, importing `App.js`. `App.js` is a branch, importing `Button.js` and `api.js` (leaves).
This is critical because modern "bundlers" like Vite and Webpack read this tree. They can see exactly which functions you *actually* import and use. Any `export`ed functions you *don't* import are considered "dead code" and are automatically removed from the final production file. This optimization, called tree shaking, is only possible because of the static `import`/`export` syntax.
2. Frameworks are 100% Module-Based
In a framework like React, **every component is a module**. You write a `Button` component in `Button.jsx`, `export default` it, and then `import Button from './Button.jsx'` in your `App.jsx` to use it. This is the entire paradigm.
✔️ Good Practice (React)
// Button.js
export default function Button() {
return <button>Click</button>;
}
// App.js
import Button from './Button.js';
function App() {
return <Button />;
}Each component is a self-contained, importable module.
❌ Bad Practice (Old Way)
// app.js
function Button() {
return <button>Click</button>;
}
function App() {
return <Button />;
}
// (Everything is in one global file)
No separation, leads to conflicts and unmanageable files.
3. Named vs. Default: A Strategy
Choosing between `default` and `named` exports is a key part of your team's strategy.
- Use `export default` for the **primary thing** a file provides. If the file is `Button.js`, its default export should be the `Button` component. A file can only have *one* default export.
- Use `export const` (Named) for **secondary utilities** or a "toolbox" file. A file like `utils.js` might export *many* helper functions: `export const formatDate`, `export const formatCurrency`, `export const truncate`.
// utils.js (A "toolbox" of named exports)
export const formatDate = () => { /*...*/ };
export const formatCurrency = () => { /*...*/ };
// App.js (Importing only what it needs)
import { formatDate } from './utils.js';Key Takeaway: Mastering modules is non-negotiable for a modern developer. It's how you structure components, share utilities, and enable optimizations that make your applications fast and maintainable.