Angular Standalone Components and their impact on modularity
Tags: angular
Published: 2022-01-26 08:59:02

## Table of Contents
  * [1. What are Standalone Components?](#what-are-sc)
  * [2. How to modularise Standalone Components](#how-to)
    * [2.1 What is a module?](#what-is-a-module)
    * [2.2 Is NgModule a module?](#is-ng-module-a-module)
    * [2.3 How to implement modules in Angular?](#how-to implement-modules)
  * [3. How do I prepare best for the migration?](#migration)
  * [4. Summary](#summary)
  * [5. Further Reading](#further-reading)

One of the upcoming features in the Angular framework will be "Standalone Components" (SC) or "Optional NgModules". It will remove the necessity for NgModules.

There are many blog posts, articles, etc. about SC. This article answers a question that isn’t discussed that often: How will SC affect modularity in an Angular application?

NgModule contains the term module. When SC makes NgModules optional and maybe deprecates them in the long run, does that mean we will not have modules anymore? Given that Angular is an enterprise framework, and the Angular team’s continuous strive for stability, this would be an unexpected move.

I start with a summary of what SC are and what advantages they bring. Then I focus on the main question, namely if optional NgModules and modularity form a contradiction. The last part is about the best way we can prepare for SC right now.

The source code is available on 
{% github rainerhahnekamp/angular-standalone-components-and-modularity no-readme %}

If you prefer watching over reading, here is the video version:

{% youtube rproG1_TCek %}

## 1. What are Standalone Components? <a name="what-are-sc"></a>

Discussions around SC have been going on for several months in the community. [Igor Minar](https://twitter.com/IgorMinar), one of the key developers behind Angular, said that he had wanted to deal with NgModules since the very early beta version of Angular. This was in 2016. So, it was quite an event when [Pawel Kozlowski](https://twitter.com/pkozlowski_os) posted the official RFC for Standalone Components on GitHub.

The key element in Angular is the component. Every component belongs to an NgModule which provides the dependencies for it. The property declarations of an NgModule's decorator creates this relationship.

For example, if the component requires the `formGroup` directive, the NgModule supplies that directive via the `ReactiveFormsModule`.

The same rule applies to the other visual elements which are Pipe and Directive. For the sake of simplicity, these two are included when we talk of a component.

This isn't just extra overhead. Given that additional link between Component and Module and the fact that an NgModule can declare multiple components,  it is not so easy to figure out which dependencies a particular component requires.

![NgModule declaring multiple Components](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/42qmius506ldp5fysr8s.png)

Besides components, there are also Services to consider - and three different ways how to provide them. The NgModule can do it, the component can do it, or the Service could provide itself via the `providedIn` property. The last option is the preferred way and was introduced in Angular 6.

So, we see that even a single component containing a form and a service involves a relatively high level of complexity.

![Component with NgModule and Service](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z2rpgdf82t8zpe6571z7.png)

Standalone Components remove the additional layer of the NgModule.

A component's decorator will receive additional properties for that. Providing Services will also become easier since there will be only two options.

![Standalone Component](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m3m9273lstxb23p0amrt.png)

## 2. How to modularise in Standalone Components? <a name="how-to"></a>

Do NgModules enable modularity in Angular applications? And if yes, should we now write our applications without modules?

### 2.1 What is a module? <a name="what-is-a-module"></a>

A good definition of a module would be a group of elements in an application that belong together. There are different possibilities for "belonging together". It could be a group that contains only presentational components, a group that contains all relevant elements for an NgRx feature state, or some other criteria.

The most important functionality of a module is encapsulation. A module can hide certain elements from the outside. Encapsulation is key for a stable architecture because it prevents every element from accessing any other element.

### 2.2 Is NgModule a module? <a name="is-ng-module-a-module"></a>

So, is the NgModule a module in that sense? Unfortunately, the NgModule fulfills these requirements only partially. It provides encapsulation at least for the visual elements (Component, Directive, Pipes) but it cannot enforce them. Theoretically, I can create a component that extends from an encapsulated one, create a new selector and voilà. Nothing prevents me from accessing a not exported class.

{% gist https://gist.github.com/rainerhahnekamp/fbe902f49b4dc875b49ae16f393ebb8d %}

It doesn't get better with Services. As described above, they can live outside of the control of an NgModule.

Since NgModules cannot deliver full modularity, we can already answer the main question of this article: 

**Standalone Components or Optional Modules will not have an impact on an application's modularity.**

Nevertheless, we have now a new question: What should we have been using for modules all this time?

### 2.3 How to implement modules in Angular? <a name="how-to implement-modules"></a>

There is something else in Angular besides NgModule, but it disguises itself under a different name. It is the library or just lib. Since Angular 6, the Angular CLI supports the generation of libraries.

A library gets its own folder next to the actual app's folder. The library also has a so-called barrel file `index.ts` where encapsulation happens. Everything that is exported from that `index.ts` is exposed to the outside. That everything can be Services, TypeScript Interfaces, functions, or even NgModules.

A sidenote about NgModules in libraries: Until SC are available, we still need the NgModule to expose components. That is why a library includes NgModules as well.

{% gist https://gist.github.com/rainerhahnekamp/c3241a799440bbe615a44360d6dbd036 %}

What about enforcing encapsulation? It can happen any time a developer imports a non-exposed file from a library. With a modern IDE that can happen very quickly. We often see this when the non-exposed elements are imported via a relative path whereas the exposed ones are imported by using the library's name.


![Linting errors due to deep-import](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4pu21nua6lp3vvwvljh9.png)

Unfortunately, there is nothing in the Angular CLI that would prevent us from doing that. Which is where nx steps in. Nx is an extension to the Angular CLI and provides, among many features, a linting rule for modularity. This linting rule throws an error if a so-called deep import, i.e. direct access to a non-exposed file, happens. See this excellent article for more information.

Nx provides another linting rule where we can also define dependency rules between modules. We can come up with rules like module A can access module B & C but module B can only access C. These rules are also validated via linting.

**So, it is the library (coupled with nx) and not the NgModule, that fulfills the requirements for a module.**

## 3. How do I prepare best for the migration?

We don't have SC yet, but can we prepare for them now to make the migration as smooth as possible?

For quite some time, and long before SC were announced, the pattern Single Component Angular Module or "SCAM" has been popular in the community. With SCAM, an NgModule declares only one component.

If you use SCAM already, the effort to migrate to SC will probably be just moving the `imports` and `providers` properties to the `@Component` decorator. A script can do this task automatically. You can find more information [here](https://netbasal.com/aim-to-future-proof-your-standalone-angular-components-accb574d273f).

Should you apply SCAM to an existing application? If you have a big application and a big desire to move to SC as quickly as possible, then SCAM can help you achieve that. In general, I would just wait until SC is released.

There is also a [shim](https://stackblitz.com/edit/ng-standalone?file=standaloneShim.ts) that provides SC right now. This shim is only for demonstration purposes and is not safe for production.

## 4. Summary <a name="summary"></a>

Dependency Management in Angular comes in different variations. This can potentially reduce consistency and is an obstacle for newcomers. The NgModule, in particular, creates unnecessary overhead. Standalone Components (Optional NgModules) will eliminate NgModules and will be a big improvement.

Optional NgModules will have essentially no impact on modularity provided by libraries. For applications following the SCAM pattern, a script can do the migration automatically. Without SCAM, you will have to do it manually.

---

I would like to thank Pawel Kozlowski for reviewing this article and providing valuable feedback.

## 5. Further Reading <a name="further-reading"></a>

- [Igor Minar on Twitter: "The story behind the Angular proposal for standalone Components, Directives, and Pipes (aka optional NgModules). It's a long one... 🧵" / Twitter](https://twitter.com/IgorMinar/status/1447593478834188291)
- [🎯AIM to Future-Proof Your Standalone Angular Components | by Netanel Basal | Netanel Basal](https://netbasal.com/aim-to-future-proof-your-standalone-angular-components-accb574d273f)
- [Emulating tree-shakable components using single component Angular modules - DEV Community](https://dev.to/this-is-angular/emulating-tree-shakable-components-using-single-component-angular-modules-13do)
- [Taming Code Organization with Module Boundaries in Nx | by Miroslav Jonaš | Dec, 2021 | Nrwl](https://blog.nrwl.io/mastering-the-project-boundaries-in-nx-f095852f5bf4)
- [RFC: Standalone components, directives and pipes - making Angular's NgModules optional · Discussion #43784 · angular/angular · GitHub](https://github.com/angular/angular/discussions/43784)
- https://stackblitz.com/edit/ng-standalone?file=standaloneShim.ts
- [Angular's Future Without NgModules - Part 2: What Does That Mean for Our Architecture? - ANGULARarchitects](https://www.angulararchitects.io/aktuelles/angulars-future-without-ngmodules-part-2-what-does-that-mean-for-our-architecture/)