What are Modules?0%

What are Modules?

Beginner12 min read•Updated: Jul 11, 2026
Study Materials

What are Modules? Modern JavaScript Architecture

As JavaScript applications expanded from simple interactive button scripts into massive, full-scale enterprise software systems, managing code across thousands of lines became impossible without modularization. JavaScript Modules provide encapsulation, scope isolation, and explicit dependency management.


1. The Historical Context: The Global Scope Crisis

In the early days of the web, every JavaScript file shared a single, global execution context:

HTML
<!-- LEGACY: Everything polluted window! -->
<script src="utils.js"></script>
<script src="auth.js"></script>
<script src="app.js"></script>

Critical Flaws of Global Scripts:

  1. 1
    Name Collisions: If utils.js defined const count = 0 and app.js declared const count = 5, the application would crash with collision errors.
  2. 2
    Order Dependency: If app.js loaded before utils.js, execution failed because dependencies were missing.
  3. 3
    Lack of Encapsulation: All internal variables and helper functions were exposed on window.

2. Evolution of Module Systems

Output
1995: Global Scripts (window.myVar)
│
2009: CommonJS (Node.js: require / module.exports)
2010: AMD / RequireJS (Asynchronous Browser Loading)
│
2015: ECMAScript Modules (ESM) — The Universal Official Standard!
(import / export)
Module SystemSyntaxPrimary EnvironmentLoading Mechanism
ES Modules (ESM)import / exportModern Browsers, Modern Node.jsStatic Analysis, Asynchronous
CommonJS (CJS)require() / module.exportsLegacy Node.jsDynamic, Synchronous

3. Core Characteristics of ES Modules

  1. 1
    Strict Mode by Default: Every ES module automatically executes in 'use strict' mode.
  2. 2
    Module-Level Scope: Variables declared at top-level in a module are private to that module and never pollute the global window object.
  3. 3
    Single Execution (Singleton): A module is executed only once when first imported; subsequent imports receive the cached exports.
  4. 4
    Deferred Execution in HTML: <script type="module"> scripts are automatically deferred until the HTML document is fully parsed.
  5. 5
    CORS Protected: Modules loaded in browsers must comply with Cross-Origin Resource Sharing (CORS) rules.

4. Enabling Modules in the Browser

To load an ES module in HTML, set type="module":

JavaScript
// main.js
import { calculateTax } from './taxCalculator.js';
 
const tax = calculateTax(100);
console.log('Tax:', tax);

Interactive Quiz

Practice Quiz

Question 1
What happens to top-level variables declared inside an ES Module?
Question 2
What HTML attribute is required to tell the browser to treat a JavaScript file as an ES Module?
Question 3
Are ES modules executed in strict mode?
Question 4
How many times is a module executed if it is imported by 5 different files in the same app?
Question 5
What legacy module format was popularized by Node.js using require() and module.exports?
Next Lesson

import & export

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

PrevNext