liveBook · Manning

Dynamic module system

As explained in The Joy of JavaScript

A module system in which dependencies and module interfaces are determined programmatically at runtime rather than by static language syntax.

A dynamic module system determines dependencies and module interfaces programmatically at runtime. Code or a module loader creates the module definition in memory, allowing conditional access to modules or parts of modules. section 6.3

Dynamic and static systems differ in how they express module boundaries:

  • CommonJS and RequireJS use ordinary JavaScript function calls such as require(...). section 6.3
  • ESM uses native import and export syntax, placing imports statically so runtimes and tools can map dependencies ahead of execution. section 6.3
  • Dynamic loading supports conditional inclusion, while static definitions support preloading and removal of unreachable package code. section 6.3

CommonJS state versus ESM bindings

A CommonJS calculator exports feePercent, netTotal, and setFeePercent. Changing a destructured feePercent locally does not change the value used by netTotal; calling setFeePercent does. The ESM version exposes the same operations, and its imported feePercent reflects the setter’s update.

How The Joy of JavaScript covers it

How deep it goes
Explains runtime module creation, conditional access, syntax differences, and optimization tradeoffs without detailing loader internals.
Examples
Uses Transaction loading comparisons and a calculator module to demonstrate loading syntax and state behavior.
What you can do after
Evaluate module-loading approaches and interpret CommonJS and ESM behavior in small JavaScript modules.
How it connects
Connects dynamic modules with CommonJS, RequireJS, SystemJS, dependency injection, and ECMAScript modules.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Joy of JavaScript. The section links point to the parts of the book it draws on.