Modules: ESM & CommonJS
Understand the idea
A file works alone but fails when copied into a browser: require is not defined. Its belongs to a different runtime setup.
A public interface for a file
As an application grows, separate files need to collaborate without every temporary name becoming global. A module makes the relationship explicit: this file provides a calculation, and that file imports it. Reading the imports gives you a first map of a file's dependencies.
Named and default exports
ES modules use import and export. Named exports must be imported by their exported name unless aliased; a default export has its own import form. Static imports belong at module top level, while dynamic import returns a and can load code on demand.
An export marks a value as part of the file's public interface. A named import uses the exported name, possibly with an alias. A default export has one default slot that the importing file can name locally. The braces in a named import select named exports; they are not destructuring of a default export.
| Feature | ES modules | CommonJS |
|---|---|---|
| Export | export / export default | module.exports / exports properties |
| Load | Static import or dynamic import() | require() |
| Normal browser support | Yes, with module scripts or dynamic import | Needs tooling or a compatible runtime |
| Binding behavior | Named imports are live, read-only bindings | Require returns the exported value/object |
| Execution timing | Static dependencies are linked before evaluation | Require loads during execution |
| Choosing a system | Follow the environment and project configuration | Do not mix syntax without checking interoperability |
ES modules in the browser
Module keeps top-level declarations out of the global scope. ESM runs in , and top-level this is . Imported bindings are live views of exported bindings, but importers cannot reassign them directly. A module normally evaluates once per module in a given runtime, so its top-level state can be shared by its importers.
CommonJS is a different module system
CommonJS uses require and module.exports and remains common in Node.js code. How Node treats a .js file depends on package configuration; .mjs and .cjs make the intended format explicit. Interoperability has rules, so do not assume a default import and a destructured require always have the same shape.
CommonJS uses require and module.exports, usually in Node.js projects. ES modules use import and export and are supported by modern browsers as modules. File extensions and a package's type setting matter in Node. If a copied snippet says require is not defined in a browser, first check whether you copied code for the wrong module environment.
Follow a dependency without leaking globals
In a browser, ESM needs a module script, valid module URLs, and appropriate server responses. Bare package names usually need a bundler or import map. The worked example dynamically imports a tiny ESM data URL so its exported interface is visible without pretending the browser provides CommonJS.
The example imports a tiny module from a data URL so the sandbox can demonstrate an actual module without extra files. A normal application would import from a file URL or a path resolved by its build tool. Dynamic import returns a promise for a module namespace object, whose exported addTax the example then calls.
Mental model
FOLLOW THE VALUE
Only the exported binding is public.
01 / 03Trace the transformation
Live example
The exported source is deliberately one line. import() resolves to a namespace , and addTax is its named exported .
Your console output appears here.
Guided challenge
Use the exported function
Call the named export with 10 instead of logging the function itself.
Your console output appears here.
EXPECTED CONSOLE OUTPUT
11
Hints 0/2
Use all hints or make 3 failed runs (0/3).
Free sandbox
Make a variation, test an assumption, or break the example on purpose. This draft is saved separately.
Your console output appears here.
Quiz
A quick check for understanding. Retry as often as you like; your learning path stays open.
Ready to call this one understood?
Mark this lesson complete to earn 200 XP. You can always revisit it.