How would you use dynamic import() to lazy-load a component-specific library?

Tests lazy loading and bundle splitting. Good answers call import() inside the component lifecycle, await the module namespace, let the bundler split the chunk, and handle loading and error states. Bad answers put import() at top level, defeating lazy loading.
WHAT THIS TESTS: This question evaluates whether you understand dynamic imports as a runtime code-splitting tool rather than just syntax trivia. The interviewer wants to see that you know import() is promise-based and lazily evaluated, that you can tie it to a component lifecycle, and that you think about the UX implications of asynchronous loading. It also checks if you trust the bundler to do its job once you use the right syntax.
A GOOD ANSWER COVERS: First, move the import from the top of the file into the component mount or render path so it only executes when the component is actually needed. Second, await the promise returned by import() and destructure the module namespace object to access the library's default or named exports. Third, note that modern bundlers like Webpack, Vite, and Rollup automatically detect import() and create a separate chunk, so no manual configuration is required in most cases. Fourth, handle the loading gap with a skeleton or spinner and wrap the call in a try-catch block to recover from network or evaluation failures. Fifth, mention that import() never throws synchronously, so all errors surface through the promise rejection.
COMMON WRONG ANSWERS: A major red flag is saying you would place the dynamic import at the module top level, which causes immediate evaluation and defeats code splitting. Another is ignoring the async nature of the returned promise and trying to use the imported bindings synchronously. Candidates also stumble by assuming dynamic imports work without a module bundler or by forgetting to mention loading and error states, which creates a janky user experience when the chunk downloads over slow networks.
LIKELY FOLLOW-UPS: The interviewer may ask how you would prefetch the chunk after initial page load but before user interaction, or how server-side rendering frameworks handle dynamic imports during hydration. They might also probe how you would implement this in a specific framework like Vue's defineAsyncComponent, Angular's loadChildren, or Svelte's await blocks. Another angle is asking how to share a dynamically imported dependency across multiple components without downloading it twice.
ONE CONCRETE EXAMPLE: Imagine a dashboard with a heavy charting library only used by an analytics modal. Instead of a static import at the top of the modal component file, you call const chartModule = await import("chart-lib") inside the modal's open handler or onMounted hook. You then instantiate the chart via chartModule.default or chartModule.createChart. While the chunk loads, the modal shows a skeleton graphic. If the request returns a 404 or the script throws, the catch block renders a fallback message. The bundler emits a separate two-hundred-kilobyte chunk that the browser only fetches when the user opens the modal.
Source: developer.mozilla.org
Read the original → developer.mozilla.org
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.