# JavaScript Modules

> Split code into files with ES modules, understand imports and exports, and know how CommonJS differs. Free notes, key concepts, resources and a mini project.

Canonical: https://www.internhack.in/roadmaps/tracks/frontend/js-modules
Last updated: 2026-11-28

Split code into files with ES modules, understand imports and exports, and know how CommonJS differs.

Beginner · about 3 hours · difficulty 2/5

Learn first: [Modern JavaScript and Error Handling](https://www.internhack.in/roadmaps/tracks/frontend/modern-javascript)

Modules let you split code into files with explicit dependencies. Every modern frontend project is a graph of modules that a bundler or the browser stitches together.

### ES modules (ESM)

- `export` named values (`export function formatDate`) or a single `export default`.
- `import { formatDate } from "./dates.js"`, `import * as utils`, and renaming with `as`.
- Modules are **strict mode**, have their own scope (no accidental globals) and are evaluated **once**; every importer shares the same instance.
- In the browser, `<script type="module">` loads ESM natively, deferred by default.
- **Dynamic import** `await import("./chart.js")` loads code on demand and is the basis of code splitting.

### CommonJS

Node's older system uses `require()` and `module.exports`. You will still see it in config files and older packages. Mixing the two causes the classic "Cannot use import statement outside a module" error; `"type": "module"` in package.json, or `.mjs`/`.cjs` extensions, decide how Node treats a file.

### Good practice

- Prefer **named exports**: they autocomplete better, are easier to rename safely and tree-shake cleanly.
- Keep modules focused. Avoid "barrel" files that re-export everything if they slow builds or hide circular imports.
- **Circular dependencies** can produce `undefined` imports at runtime; restructure so shared code lives in a third module.

Move on when you can organise a vanilla project into feature modules and lazy-load a heavy module with dynamic import.

## Key concepts

- **Named and default exports**
- **Import syntax**
- **script type=module**
- **Dynamic import()**
- **CommonJS require and module.exports**
- **type: module and .mjs/.cjs**
- **Circular dependencies**

## Mini project

Split your earlier vanilla project into ES modules (api, state, render, utils) loaded with script type=module, and lazy-load a charts module only when the user opens a stats tab.

## Check yourself

Why do tools find it easier to tree-shake named ES module exports than CommonJS exports?

## Free resources

- [JavaScript modules](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules) (MDN)
- [Modules, introduction](https://javascript.info/modules-intro) (javascript.info)

Previous: [Git and GitHub](https://www.internhack.in/roadmaps/tracks/frontend/git) · Next: [Package Managers](https://www.internhack.in/roadmaps/tracks/frontend/package-managers)

## More in Everyday Workflow and Tooling

- [Git and GitHub](https://www.internhack.in/roadmaps/tracks/frontend/git)
- [JavaScript Modules](https://www.internhack.in/roadmaps/tracks/frontend/js-modules)
- [Package Managers](https://www.internhack.in/roadmaps/tracks/frontend/package-managers)
- [Build Tools and Bundlers](https://www.internhack.in/roadmaps/tracks/frontend/build-tools)
- [Linting and Formatting](https://www.internhack.in/roadmaps/tracks/frontend/linting-formatting)

## Related

- [Roadmaps](https://www.internhack.in/roadmaps)
- [Frontend Developer Roadmap](https://www.internhack.in/roadmaps/tracks/frontend)
