Structuring Projects: JavaScript Modules

Discover the `import` and `export` syntax that powers all modern JavaScript. Learn to organize your code into clean, reusable, and maintainable modules.

Lesson ProgressStep 1 of 9
📄 math.js
📄 app.js
export
import
{ }
default
0 EXP

Welcome! Let's explore JavaScript Modules. We'll learn how `export` and `import` keep code organized.

// From chaos... to clarity!

What is a JavaScript Module?

In modern JavaScript, a module is simply a file. That's it. Each JavaScript file is its own module.

The most important concept is that each module has its own private scope. Variables, functions, or classes defined in one module are not available to any other module unless you explicitly export them. This prevents naming conflicts and keeps your code organized.

// math.js
const mySecret = 42; // This is private

export const add = (a, b) => a + b; // This is public

System Check

What is the default scope of a variable defined in an ES6 module?

Advanced Holo-Simulations

0 EXP

Log in to unlock these advanced training modules and test your skills.


Achievements

📤
Export Master

Correctly export a function using a named export.

📥
Import Whiz

Correctly import and use a function from another module.

✍️
Module Syntax Expert

Prove your mastery of default and named export syntax.

Mission: Build a Module

Use comments to simulate two files. In `utils.js`, `export` a function named `greet`. In `app.js`, `import` and call `greet()`.

A.D.A. Feedback:

> System integrity looks stable. Code is valid.

Challenge: Order the Module Flow

Drag the lines of code into the correct logical order to make these two "files" work.

import { multiply } from './math.js';
// math.js
console.log( multiply(5, 5) );
export const multiply = (a, b) => a * b;

Challenge: Complete the Syntax (Default Export)

Fill in the missing parts to connect these modules using a default export.

// utils.js
function() { console.log('Hi'); }
// app.js
import from '';

Consult A.D.A.

Community Holo-Net

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.

JavaScript Modules Glossary

Module
A single JavaScript file. In ES6 module mode, each file has its own private scope. Variables and functions are not global unless explicitly `export`ed.
`export`
The keyword used to make code (variables, functions, classes) from one module publicly available to be `import`ed by other modules.
`import`
The keyword used to bring `export`ed code from another module into the current module's scope.
Named Export
A way to export multiple values from a single file.
Example: `export const add = ...; export const subtract = ...;`
Imported with: `import { add, subtract } from './math.js';`
Default Export
A way to export a single, primary value from a file. A file can have only one default export.
Example: `export default function Button() { ... }`
Imported with: `import MyButton from './Button.js';` (The name `MyButton` can be anything).
Module Specifier
The string in an `import` statement that tells the browser/bundler where to find the module.
Example: `'./utils.js'` (relative path) or `'react'` (bare specifier, for npm packages).
Tree Shaking
An optimization by bundlers (like Vite or Webpack) that analyzes `import`/`export` statements to determine which code is unused and removes it from the final production bundle.
CommonJS
An older module system primarily used by Node.js. It uses `require()` and `module.exports` instead of `import`/`export`. ES6 Modules are the modern standard for both browser and Node.js.

About the Author

Author's Avatar

TodoTutorial Team

Passionate developers and educators making programming accessible to everyone.

This article was written and reviewed by our team of web development experts, who have years of experience teaching JavaScript and building robust, modular web applications.

Verification and Updates

Last reviewed: November 2025.

We strive to keep our content accurate and up-to-date. This tutorial is based on the latest ES6+ module specifications and is periodically reviewed.

External Resources

Found an error or have a suggestion? Contact us!