Skip to content
Modern JavaScriptChapter 42 / 47Intermediate5 min read

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 modulesWith modules
All variables in the global scopeEvery file has its own scope
You had to remember the order of script tagsimport states the dependency itself
One huge file, hard to maintainSmall files, each with one job
Names clash with each otherOnly what you export is shared

3. Where are they used?

PlaceExample
React / Next.js appsEach component in its own file, import Button from "./Button"
Utility functionsExport formatPrice from utils/format.js
npm packagesimport { z } from "zod"
Node.js backendRoutes and services with "type": "module"
Code splittingLoad 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.

Module loading flow
main.js is loaded
The import statements are read
math.js and api.js are loaded
Each module is executed once
The exported values reach main.js

There are two kinds of exports:

TypeHow many per fileImport syntax
Named exportAs many as you likeimport { add } from "./math.js"
Default exportOnly oneimport anyName from "./math.js"

5. Code example

Named and default exports

js
// math.js
export const PI = 3.14159;

export function add(a, b) {
  return a + b;
}

export default function multiply(a, b) {
  return a * b;
}
js
// 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

js
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:

html
<script type="module" src="./main.js"></script>

How does the code work?

ItemMeaning
export const PIA named export, imported under the same name
export defaultThe main export of the file, and you can give it any name when importing
import * as mathAll named exports in one object
add as sumRename 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

Final quick revision

PointDetail
ModuleA file with its own scope that exports and imports
Named exportMany per file, imported with curly braces
Default exportOne per file, imported under any name
Dynamic importimport() returns a Promise, for lazy loading
Browsertype="module" is required