{"id":1381,"date":"2023-08-29T05:00:00","date_gmt":"2023-08-29T12:00:00","guid":{"rendered":"https:\/\/www.angulartraining.com\/daily-newsletter\/?p=1381"},"modified":"2023-08-28T15:19:57","modified_gmt":"2023-08-28T22:19:57","slug":"what-you-need-to-know-about-ngmodules","status":"publish","type":"post","link":"https:\/\/www.angulartraining.com\/daily-newsletter\/what-you-need-to-know-about-ngmodules\/","title":{"rendered":"What you need to know about ngModules"},"content":{"rendered":"\n<p>Angular modules (<a href=\"https:\/\/angular.io\/guide\/module-types\" target=\"_blank\" rel=\"noopener\" title=\"\">ngModules<\/a>) are a source of confusion for Angular developers. This short guide will clarify what they are and how to think about <code>ngModules<\/code> in general.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why ngModules?<\/h2>\n\n\n\n<p>Angular uses a Typescript compiler and a template compiler that turns our HTML templates into Javascript instructions to render the DOM in a browser.<\/p>\n\n\n\n<p>The Typescript compiler knows how to compile our Typescript code (all our classes) because all dependencies have to be imported in these Typescript files:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"784\" height=\"336\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/08\/image-36.png\" alt=\"\" class=\"wp-image-1382\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/08\/image-36.png 784w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/08\/image-36-300x129.png 300w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/08\/image-36-768x329.png 768w\" sizes=\"auto, (max-width: 784px) 100vw, 784px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>Compiling the HTML template for the above component is a different task because <code>*ngIf<\/code> is not imported anywhere in our Typescript code. Yet, the Angular compiler must know about all directive\/component\/pipe dependencies of a component&#8217;s template to turn all that HTML into DOM instructions.<\/p>\n\n\n\n<p>The solution to that problem was the introduction of <code>ngModules<\/code>.  <code>ngModules<\/code> expose features meant to be used by the template compiler and does so in a &#8220;global&#8221; way, in the sense that importing an <code>ngModule<\/code> once is enough to enable all its code for all components within that module.<\/p>\n\n\n\n<p>That&#8217;s why we rarely think twice about using <code>ngFor<\/code> or <code>ngIf<\/code> or any pipe: They are all part of <code>CommonModule<\/code>, which is automatically imported into the <code>AppModule<\/code> by default:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"350\" height=\"295\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/08\/image-37.png\" alt=\"\" class=\"wp-image-1384\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/08\/image-37.png 350w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/08\/image-37-300x253.png 300w\" sizes=\"auto, (max-width: 350px) 100vw, 350px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Do I need to create ngModules in my app?<\/h2>\n\n\n\n<p>Most likely, no. The Angular team introduced <a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/what-are-standalone-components\/\" target=\"_blank\" rel=\"noopener\" title=\"\">standalone components<\/a> as an alternative to <code>ngModules<\/code>. A standalone component does not need such modules because all its dependencies are listed in the Typescript code itself:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"767\" height=\"512\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/08\/image-38.png\" alt=\"\" class=\"wp-image-1385\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/08\/image-38.png 767w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/08\/image-38-300x200.png 300w\" sizes=\"auto, (max-width: 767px) 100vw, 767px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>There were only two reasons why you&#8217;d need to create your own ngModules in the past:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>You needed to share many components\/directives\/pipes\/services as a library. Packaging those in an <code>NgModule<\/code> was the only option.<\/li>\n\n\n\n<li>You needed to <a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/lazy-loading-for-better-angular-performance\/\" target=\"_blank\" rel=\"noopener\" title=\"\">lazy-load a bunch of features to improve performance<\/a>.<\/li>\n<\/ul>\n\n\n\n<p>That&#8217;s it. Both of these problems are solved by standalone components, which <a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/lazy-loading-standalone-components\/\" target=\"_blank\" rel=\"noopener\" title=\"\">can be lazily loaded<\/a> and already bring all their dependencies along, so no <code>ngModule<\/code> is needed.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What about core, shared, and feature modules?<\/h2>\n\n\n\n<p>Those were parts of <a href=\"https:\/\/angular.io\/guide\/module-types\" target=\"_blank\" rel=\"noopener\" title=\"\">guidelines created for the sake of consistency within Angular applications<\/a>. But you don&#8217;t need these modules for your application to work. You can still organize your code neatly in folders and sub-folders and not use ngModules. You can even have tidy, <a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/path-mapping-to-simplify-verbose-imports\/\" target=\"_blank\" rel=\"noopener\" title=\"\">short import statements<\/a> without having <code>ngModules<\/code>.<\/p>\n\n\n\n<p>Usually, the more ngModules you create, the more work and problems you&#8217;re likely to encounter (circular dependencies, anyone?), which is why the Angular team introduced standalone components and already migrated all <a href=\"https:\/\/github.com\/angular\/angular\/blob\/16.2.2\/packages\/common\/src\/directives\/ng_if.ts#L151-L154\" target=\"_blank\" rel=\"noopener\" title=\"\">its internal directives<\/a> and pipes to standalone. In the long run, ngModules will likely disappear.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Angular modules (ngModules) are a source of confusion for Angular developers. This short guide will clarify what they are and how to think about ngModules in general. Why ngModules? Angular uses a Typescript compiler and a template compiler that turns our HTML templates into Javascript instructions to render the DOM in a browser. The Typescript [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,21,23],"tags":[],"class_list":["post-1381","post","type-post","status-publish","format-standard","hentry","category-angular","category-architecture","category-standalone"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/1381","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/comments?post=1381"}],"version-history":[{"count":3,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/1381\/revisions"}],"predecessor-version":[{"id":1387,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/1381\/revisions\/1387"}],"wp:attachment":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/media?parent=1381"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/categories?post=1381"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/tags?post=1381"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}