Modules: import & export
Learn JavaScript modules with import and export, the difference between named and default exports, and how to split your code into files.
On this page
1. What are modules?
Every module has its own scope. A variable or function you do not export is not visible outside the file. This system is called ES Modules (ESM), and it is built into both the browser and Node.js.
2. Why do they matter?
In the old days, all code lived in one huge file or in many script tags. Every variable was global, and names clashed with each other.
| Without modules | With modules |
|---|---|
| All variables in the global scope | Every file has its own scope |
| You had to remember the order of script tags | import states the dependency itself |
| One huge file, hard to maintain | Small files, each with one job |
| Names clash with each other | Only what you export is shared |
3. Where are they used?
| Place | Example |
|---|---|
| React / Next.js apps | Each component in its own file, import Button from "./Button" |
| Utility functions | Export formatPrice from utils/format.js |
| npm packages | import { z } from "zod" |
| Node.js backend | Routes and services with "type": "module" |
| Code splitting | Load heavy code later with import() |
4. How do they work?
When the browser or a bundler loads a module, it first reads all the import statements and builds a dependency graph. Then each module runs only once, no matter how many files import it.
There are two kinds of exports:
| Type | How many per file | Import syntax |
|---|---|---|
| Named export | As many as you like | import { add } from "./math.js" |
| Default export | Only one | import anyName from "./math.js" |
5. Code example
Named and default exports
// math.js
export const PI = 3.14159;
export function add(a, b) {
return a + b;
}
export default function multiply(a, b) {
return a * b;
}// main.js
import multiply, { add, PI } from "./math.js";
import * as math from "./math.js";
console.log(add(2, 3));
console.log(multiply(4, 5));
console.log(math.PI);Renaming and dynamic import
import { add as sum } from "./math.js";
button.addEventListener("click", async () => {
const { formatReport } = await import("./report.js");
console.log(formatReport(sum(1, 2)));
});To run a module in the browser, you need type="module" on the script tag:
<script type="module" src="./main.js"></script>How does the code work?
| Item | Meaning |
|---|---|
export const PI | A named export, imported under the same name |
export default | The main export of the file, and you can give it any name when importing |
import * as math | All named exports in one object |
add as sum | Rename while importing |
await import() | Dynamic import, the file loads only when needed and it returns a Promise |
Interview tip
Interviewers often ask the difference between ES Modules and CommonJS. Short answer: ESM uses import / export and is static (build tools can do tree shaking), while CommonJS uses require / module.exports and runs at runtime. ESM is the standard in new projects.
6. Common mistakes
Mistake: Importing a named export without curly braces
Why? import add from ./math.js looks for the default export, so named add is not found and you get undefined or an error.
Better: Use curly braces for a named export, like import { add } from ./math.js.
Mistake: Forgetting type module in the browser
Why? In a normal script, the import statement gives a syntax error.
Better: Add type=module to the script tag, or use a bundler like Vite.
Mistake: Leaving out the extension in Node.js
Why? In native ESM, Node does not guess the extension of a relative path, so you get a module not found error.
Better: Write the full path in Node ESM, like ./math.js. Bundlers like Vite work even without the extension.
Mistake: Using only default exports in every file
Why? Each import can use a different name, and the editor's auto import and refactoring get weaker.
Better: Named exports are better for utilities. Keep default exports for one main thing.
7. What to learn before and after
| Stage | Topics |
|---|---|
| Before | Functions, Scope & Hoisting, Objects |
| Alongside | Destructuring, Async / Await |
| After | npm & Packages, React Setup with Vite |
Final quick revision
| Point | Detail |
|---|---|
| Module | A file with its own scope that exports and imports |
| Named export | Many per file, imported with curly braces |
| Default export | One per file, imported under any name |
| Dynamic import | import() returns a Promise, for lazy loading |
| Browser | type="module" is required |