NgModules — the legacy architecture

Intermediate · 10 min

What NgModule did, why Angular moved away from it, and how to read a codebase that still uses it.

Before thisThe CLI, project anatomy, and standalone bootstrapping

procademy · 10 min

NgModules — the legacy architecture

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by procademyReport a problem with this video
Not started
Set this lesson's status

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.” CommonModule for NgIf/NgFor-era directives, RouterModule for routing directives, SharedModule for whatever your team grouped together.
  • exports — “of what I declared, here’s what I’ll let other modules use.” UserListComponent is declared but not exported, so it’s private to this module; UserCardComponent is exported, so any module that imports UserModule can use it in its own templates.
  • providers — services this module registers with the injector. (The standalone equivalent — providers in ApplicationConfig, or a component’s own providers array — 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

  1. 01In 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.

  2. 02If 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

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark