liveBook · Manning

Module graph

As explained in Secrets of the JavaScript Ninja, Third Edition

A graph representing modules as nodes and their import dependencies as directed edges.

A module graph represents modules as nodes and their import dependencies as directed edges. Bundlers analyze this graph to decide what code to remove, combine, or split into chunks. section 13.3 section 13.2.2

How the graph affects JavaScript builds and execution:

  • Static ESM imports let tooling resolve dependencies before execution, making it practical to analyze the graph and prune unused code. section 13.0 section 13.3
  • CommonJS require() can run conditionally or construct its path at runtime, so the dependency graph cannot be determined in the same static way. section 13.0 section 13.2.2
  • Top-level await makes ESM modules asynchronous: modules waiting on async dependencies can change execution order, while other modules proceed. section 13.2.2

One graph, several chunks

A website can place shared libraries such as React in one chunk and each page’s content in a separate chunk. The bundler uses module dependencies to organize those outputs. section 13.3

// Require 1.js, 2.js, or 3.js at random
require(`./${Math.ceil(Math.random() * 3)}.js`);

This runtime-built require() path illustrates why a CommonJS dependency may not be visible to static graph analysis. section 13.2.2

How Secrets of the JavaScript Ninja, Third Edition covers it

How deep it goes
Explains static dependency resolution and async execution effects, while deferring bundler-specific implementation details.
Examples
Uses shared React and page-specific chunks, plus a runtime-built CommonJS require() path.
What you can do after
Understand how static ESM dependencies support bundler analysis and why dynamic CommonJS loading complicates it.
How it connects
Connects module graphs to bundlers, ECMAScript Modules, CommonJS, tree-shaking, and top-level await.

Related concepts

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