Skip to content
tezvyn:

How would you implement lazy loading for an Angular feature module?

Source: angular.devHardHow cards are made

How would you implement lazy loading for an Angular feature module?

This tests route-level code splitting and CLI workflows. A great answer hits: ng generate module, loadChildren in the route config, and excluding the feature from AppModule. A red flag is suggesting manual chunking instead of router-driven lazy loading.

What's really being asked

The interviewer wants to know if you understand how Angular's router enables code splitting at the route level and how the CLI supports this workflow. It is not enough to know the syntax; they care whether you understand the build-time implications, the module graph, and the difference between eager and lazy-loaded chunks. This is a fundamental performance optimization for large Angular applications.

The full answer

A strong response starts with the CLI command, specifically ng generate module FeatureModule --route=feature --module=app, which scaffolds the module and a default component and wires the lazy-loaded route in one step. Next, it explains the routing change: in the parent route configuration, you replace a direct component reference or eager module import with loadChildren. For NgModule-based apps, this means using a dynamic import function returning the module class, such as loadChildren: () => import('./feature/feature.module').then(m => m.FeatureModule). It also covers the critical step of removing any eager import of that feature module from AppModule or other eagerly loaded modules, because if the module remains in the static dependency graph, the bundler cannot tree-shake it into a separate chunk. Finally, a senior candidate mentions that the Angular CLI and Webpack handle the actual chunk creation automatically when it detects the dynamic import, so no manual bundle configuration is needed.

The mistakes people make

A major red flag is suggesting manual Webpack entry points or externals to solve the problem, which shows a misunderstanding of Angular's abstraction over the build tool. Another mistake is describing loadChildren but forgetting to remove the module from AppModule imports, which would nullify the optimization. Some candidates conflate standalone component lazy loading, which uses loadComponent, with the traditional NgModule-based loadChildren pattern, or they suggest using the string-based loadChildren syntax from older Angular versions without mentioning the modern dynamic import approach.

What usually comes next

The interviewer may ask how you would handle preloading strategies to speed up subsequent navigations without hurting initial load. They might also ask how to share dependencies between lazy-loaded chunks to avoid duplicate code, or how to protect lazy routes with guards. Another common pivot is asking about the difference between loadChildren and loadComponent in standalone applications, or how to measure the actual bundle size reduction.

A concrete example

Suppose you have a SettingsModule that is only accessed by administrators. You would run ng generate module settings --route=settings --module=app. The CLI creates a settings folder with SettingsModule and SettingsComponent, and it automatically adds a lazy route to AppRoutingModule. You then verify that AppModule does not import SettingsModule. When the application builds, the CLI emits a separate chunk, such as src_app_settings_settings_module_ts.js, which the browser only fetches when the user navigates to the settings path. This keeps the main bundle smaller and improves the initial load time metric.

Interview question

When implementing route-level lazy loading for an NgModule, which combination ensures the CLI emits a separate chunk without manual build configuration?

  • a.The feature uses loadChildren with a dynamic import and a custom Webpack entry point
  • b.The feature uses loadChildren with a dynamic import and is removed from AppModule importsCorrect
  • c.The feature uses loadComponent with a dynamic import while remaining in AppModule imports
  • d.The feature uses a string-based loadChildren path and is declared in AppModule declarations
Why?

The CLI automatically splits a chunk when it detects a dynamic import in loadChildren and the module is absent from AppModule's static dependency graph. Manual Webpack entry points are a red flag because Angular abstracts the build tool, and keeping an eager import in AppModule forces the feature into the main bundle.

Just read this? Test yourself on what you have been reading.

Read the original → angular.dev

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on angular — each one lists the topics its interview covers.

See open roles