Lazy loading and dynamic import boundaries
Quick answer
Dynamic import returns a promise for a module namespace. A build tool can use a statically analyzable dynamic import as a boundary for splitting optional features into separate chunks.
Tooling examples
Example 1. Context: projects/build-lab. Run in a fresh prepared fixture copy; see its README. Save as example.mjs in that folder; run node example.mjs. Local check available; consult validation evidence for what was asserted.
console.log("start");const report = await import("./report.mjs");console.log(report.summary());Expected observation: start report loaded ready
Example 2. Context: projects/build-lab. Run in a fresh prepared fixture copy; see its README. Save as example.mjs in that folder; run node example.mjs. Local check available; consult validation evidence for what was asserted.
const a = await import("./report.mjs");const b = await import("./report.mjs");console.log(a === b);Expected observation: report loaded true
How the tooling works
Load rarely used functionality when it is needed and handle rejected imports. Module evaluation and network loading are distinct; native modules are normally evaluated once for the same resolved module identity. A bundler may share or combine chunks depending on configuration, so one import expression does not guarantee one output file. Splitting can reduce initial bytes but add requests, loading states and failure modes. Measure a real build before optimizing.
Common mistakes
Do not construct import paths directly from untrusted input. Choose from an explicit known feature map and handle errors.
Practice
Add a button in a browser lab that loads a report once requested; provide loading and error states. Inspect actual chunks only in a working bundler environment.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Lazy loading and dynamic import boundaries — Knowledge Check
Assess tooling contracts, diagnostic reasoning and transfer to a project.
What does import("./report.mjs") return immediately?
What failure should a lazy feature handle?
Does every dynamic import guarantee a separate output chunk?
In Lazy loading and dynamic import boundaries, what is the expected observation for example 1 in its documented setup? console.log("start"); const report = await import("./report.mjs"); console.log(report.summary());
In Lazy loading and dynamic import boundaries, what is the expected observation for example 2 in its documented setup? const a = await import("./report.mjs"); const b = await import("./report.mjs"); console.log(a === b);