NgModules — the legacy architecture
What NgModule did, why Angular moved away from it, and how to read a codebase that still uses it.
NgModules — the legacy architecture
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- An `NgModule` is a declaration of four lists — `declarations`, `imports`, `exports`, `providers` — spelling out what a slice of the app has, uses, shares, and provides.
- Standalone components replace `declarations` and `imports` — a component lists its own dependencies instead of a module declaring it alongside its neighbors.
- You'll still meet NgModules constantly in real codebases and in some third-party libraries — understanding them isn't optional just because new projects don't generate one.
A module answers a question standalone components don’t need to ask
Before standalone components existed, every component, directive, and pipe had to
belong to exactly one NgModule before Angular would even compile it. That
requirement came from a real constraint of the time: Angular needed some unit
bigger than “one component” to answer questions like “what’s allowed to use what,”
“which third-party library’s components are available here,” and “what services
get provided to this whole slice of the app.” A single component can’t answer
those questions about itself — it needs something a level up to declare the
boundary.
NgModule was that boundary. Once components stopped needing a module to be
compiled at all — once standalone: true (and later, the default) meant a
component could declare its own dependencies — that boundary stopped being
required. But it didn’t stop existing; most real Angular code written before
roughly 2023 still uses it, and plenty of libraries still ship module-based public
APIs. You need to be able to read one even if you never write one.
Reading an NgModule as four separate lists
@NgModule({
declarations: [UserListComponent, UserCardComponent],
imports: [CommonModule, RouterModule, SharedModule],
exports: [UserCardComponent],
providers: [UserService],
})
export class UserModule {}
Each array is answering its own question:
declarations— “these components, directives, and pipes belong to this module; compile them here.” A component can only be declared in one module, ever.imports— “this module needs these other modules’ exported stuff to be available.”CommonModuleforNgIf/NgFor-era directives,RouterModulefor routing directives,SharedModulefor whatever your team grouped together.exports— “of what I declared, here’s what I’ll let other modules use.”UserListComponentis declared but not exported, so it’s private to this module;UserCardComponentis exported, so any module that importsUserModulecan use it in its own templates.providers— services this module registers with the injector. (The standalone equivalent —providersinApplicationConfig, or a component’s ownprovidersarray — does the same job without a module as the middleman.)
This lesson’s video walks through building one of these from scratch, which is the fastest way to see why each array exists — watch for the moment a component fails to render because it was used somewhere it was never exported to.
Why standalone won
The module system worked, but it added a layer of indirection to a question that
didn’t need one: “what does this component depend on?” With modules, the answer
was “trace back to find which module declared it, then read that module’s
imports.” With a standalone component, the answer is right there on the
component itself:
@Component({
standalone: true,
imports: [NgIf, RouterLink, UserCardComponent],
selector: 'app-user-list',
// ...
})
export class UserListComponent {}
No module to go hunting for. The component is the unit, full stop — it’s portable in a way a declared-but-not-exported component never was, because it carries its own dependency list instead of relying on something else to have already set that relationship up.
What you’ll actually run into
You won’t need to write a new NgModule in this roadmap’s examples from here on.
But you will run into RouterModule.forRoot()-style APIs in older tutorials,
SharedModule grab-bags in real codebases, and libraries whose public API is
still a module you’re expected to import. Reading this lesson’s four-list
breakdown is what lets you translate those into the standalone mental model you
already have: a module’s declarations are just components that haven’t been
given their own imports array yet.
Check yourself
In an NgModule, what's the difference between putting a component in `declarations` versus `exports`?
`declarations` says this module owns and compiles that component. `exports` says other modules that `import` this one are also allowed to use it — without `exports`, a declared component is private to its own module.
If a component is standalone, what replaces an NgModule's `declarations` and `imports` arrays?
The component's own `imports` array, right on its `@Component` decorator — it lists the other standalone components, directives, and pipes it uses directly, with no module in between declaring either side of that relationship.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.