What are Modules?0%
What are Modules?
Beginner12 min read•Updated: Jul 11, 2026
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:
- 1Name Collisions: If
utils.jsdefinedconst count = 0andapp.jsdeclaredconst count = 5, the application would crash with collision errors. - 2Order Dependency: If
app.jsloaded beforeutils.js, execution failed because dependencies were missing. - 3Lack 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 System | Syntax | Primary Environment | Loading Mechanism |
|---|---|---|---|
| ES Modules (ESM) | import / export | Modern Browsers, Modern Node.js | Static Analysis, Asynchronous |
| CommonJS (CJS) | require() / module.exports | Legacy Node.js | Dynamic, Synchronous |
3. Core Characteristics of ES Modules
- 1Strict Mode by Default: Every ES module automatically executes in
'use strict'mode. - 2Module-Level Scope: Variables declared at top-level in a module are private to that module and never pollute the global
windowobject. - 3Single Execution (Singleton): A module is executed only once when first imported; subsequent imports receive the cached exports.
- 4Deferred Execution in HTML:
<script type="module">scripts are automatically deferred until the HTML document is fully parsed. - 5CORS 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
| Previous Lesson | Next Lesson |
|---|---|
| Error Handling in Async Code | import & export |
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.