ES Modules
- Modules. A module is one file with its own top-level scope: nothing leaks out unless the file exports it.
- export. The export keyword marks what a file offers: named exports for several values, default for the file’s one main thing.
- import. The import statement pulls another module’s exports into scope, and the engine loads each module exactly once.
Imports, Exports and Dependencies
- Dependency graphs. Imports form a graph: the entry file names what it needs, those files name theirs, and the loader walks it all.
- Circular imports. Two modules importing each other create a cycle, which loads but can hand one side half-initialised values.
- Packages. A package is a published folder of modules with a name and version, installed into node_modules and imported by name.
Designing Maintainable Modules
- Single purpose. A maintainable module has one job you can state in a sentence: parsing dates, talking to the orders API.
- Small surfaces. Export the minimum: every extra name is surface other code will grip, making later change harder.
- Layering. Point dependencies one way: interface code may import data code, never the reverse: and cycles stop appearing.
Before and after this level
- Before: Level 6: Objects & Prototypes: Prototypes and Prototype Chains, Classes and Object-Oriented JavaScript, Composition and Encapsulation.
- Next: Level 8: Errors & Debugging: Exceptions and Error Handling, Debuggers and Stack Traces, Defensive JavaScript.
In the app each of these ideas is taught and then asked back, and the level ends with a checkpoint and flashcards. See Checkpoints and flashcards.
Frequently asked questions
What does JavaScript Level 7: Modules & Code Organisation cover?
ES Modules, Imports, Exports and Dependencies, Designing Maintainable Modules: 3 lessons and 9 ideas.
What should I know before this level?
The level before it, Level 6: Objects & Prototypes, which covers Prototypes and Prototype Chains, Classes and Object-Oriented JavaScript, Composition and Encapsulation.