Modules

Splitting code across multiple files, and sharing pieces between them deliberately.

What is it?

Real apps quickly outgrow one file. Modules let you split code into files that each do one thing, and control what they share: export makes something available to other files, and import brings it in.

Explain like I'm 10

Think of departments in a company. Each department (file) works on its own and shares only specific documents (exports) with departments that ask for them (imports). Nobody has to see everything.

Examples

Exporting and importing

// math.js
export function add(a, b) {
  return a + b;
}
export const PI = 3.14159;

// app.js
import { add, PI } from "./math.js";

console.log(add(2, 3)); // 5
console.log(PI);        // 3.14159

A default export

// user.js
export default class User {
  constructor(name) {
    this.name = name;
  }
}

// app.js
import User from "./user.js"; // any name works for a default import

const amara = new User("Amara");

A module can have at most one default export, and the importer can name it anything. Named exports must be imported by their exact name.

How it works

Each file is a module with its own private scope. Nothing in it is visible elsewhere unless exported. When a file imports from another, JavaScript loads that module (once, even if many files import it), runs it, and hands over the exported bindings you asked for.

Why does it exist?

Without modules, all files share one global scope. Names collide, and you can't tell what depends on what by reading a file. Modules give each file its own scope and make dependencies explicit.

When to use it

Split code into modules once a file covers more than one clear job: a set of utility functions, a component, a group of related constants. Import only what each file needs.

When not to use it

For a tiny script, splitting into modules can add more overhead (import paths to manage) than it saves. Avoid circular imports (A imports B, which imports A); they're a common source of confusing bugs.

Common mistakes

  • Forgetting the file extension or path in an import in environments that require it.

  • Exporting far more than a module actually needs to share, making its real public surface unclear.

  • Creating circular imports between two modules that depend on each other.

Practice exercises

  1. Easy:

    Create a module that exports a greet(name) function, and import it into another file to use it.

  2. Medium:

    Create a module with both a default export and a couple of named exports, and import all of them correctly.

  3. Hard:

    Split a small script's logic into three modules with clear, single responsibilities, importing only what each one needs.

Interview questions

What's the difference between a named export and a default export?

A module can have many named exports (imported with exact names in {}) but only one default export (imported under any name you choose).

Why are modules better than one giant global script?

They keep variables scoped to their own file by default, and make what's shared between files explicit and traceable through imports.

What is a circular dependency?

When two modules import from each other, directly or indirectly, which can cause one of them to receive an incomplete, partially-loaded version of the other.