Sub topic 1
What Makes a File a Module
An ES module is a JavaScript file that the engine loads as a module instead of as a classic script. The code inside follows different rules. Its top-level variables stay private, it always runs in strict mode, and its connections to other files are declared with import and export before any code runs. This chapter covers those rules. Later chapters explain the syntax that uses them.
Prerequisites
This chapter assumes you already know:
- Core JavaScript syntax: variables, functions, objects, and destructuring
- The difference between
let,const, andvar - Promises and
async/await - How to run JavaScript in a browser or with Node.js
The host decides, not the file
A file does not label itself as a module. The environment that loads it decides. In a browser, <script type="module"> loads the file as a module. In Node.js, a file is loaded as a module when it ends in .mjs or when the nearest package.json contains "type": "module". The same source text can behave differently depending on how it is loaded. Later chapters cover both setups in detail.
Module scope
In a classic script, a top-level var or function declaration becomes a property of the global object. In a module, every top-level declaration belongs to that module's own scope. Other files can only reach a value if the module exports it.
// scope-demo.mjs (run with: node scope-demo.mjs)
var total = 10;
console.log(total); // 10
console.log(globalThis.total); // undefined: top-level var stays in module scope
console.log(this); // undefined: top-level this is not the global object
try {
undeclared = 5; // strict mode forbids implicit globals
} catch (err) {
console.log(err.name); // ReferenceError
}Strict mode by default
Modules always run in strict mode, so you don't need "use strict". Assigning to an undeclared name throws an error, with statements are banned, and duplicate parameter names are syntax errors. Code copied from an old script can break for these reasons when you move it into a module.
Static structure
import and export declarations are allowed only at the top level of a module. They cannot appear inside if blocks or functions, and the module path in an import must be a string literal. Because of these limits, the engine can read every dependency before running any code. It builds a module graph, loads each file once, and connects them. The dynamic import() function, covered later, is the escape hatch for loading code at runtime.
Classic scripts versus modules
| Behavior | Classic script | ES module |
|---|---|---|
| Top-level `var` | Becomes a global property | Stays in module scope |
| Strict mode | Opt-in with `"use strict"` | Always on |
| Top-level `this` | Global object (non-strict) | `undefined` |
| `import` / `export` | Syntax error | Allowed at top level |
| Loaded twice | Runs twice | Evaluated once and shared |
Exercise
A module contains var apiKey = 'abc'; at the top level, and a separate classic script on the same page reads window.apiKey. What does the classic script see, and why?
Show answer
It sees undefined. A top-level var in a module belongs to the module's own scope and does not become a property of the global object, so other code can only read the value if the module exports it.
Exercise
Why is if (debug) { import './devtools.js'; } a syntax error?
Show answer
import declarations must appear at the top level of a module so the engine can find every dependency before running any code. To load a module conditionally, use dynamic import().