Angular Module vs. Library

hogan picture hogan · Jun 23, 2018 · Viewed 8k times · Source

So great, the Angular has released version 6 and the CLI provides an option to generate libraries - something that they call a "highly anticipated feature".

Now, coming from a business perspective, I am wondering:

  • why you would actually need this,
  • why inside of an existing project and not a separate which you can install as usual via npm...
  • and if you want this inside your project, why not use a module for that.

So I get it, publish something on npmjs and hope the whole world and future connected things need exactly that. Library, great.

Inside a big system in a even bigger company, we could reuse a library but we already were able to reuse stuff with a module. I need to build the lib separately and recompile every time I change something..

What are the reasons to use a library instead of a module?

(Currently I'd say that a lib can be even bigger than a module, so I would just use it to organize my application better)

Answer

hogan picture hogan · Jun 25, 2018

On this website I found a great explanation.

Modules

Reside inside of a project and let us bundle components which belong together. We can import this module where we need it instead of declaring all components at the root level. I.e. a AuthModule can take care of all auth related stuff. No need to let the app know what Components this module is using.

Libraries

In corporations there are many different applications and some/many might move to be based on angular in the future.

DevOps want ease of mind and keep things in one place, so the Angular CLI Team introduced workspaces - which can contain more than one project.

Then sharing things like an AuthModule becomes easy. We make a library of it and share it among the different projects. No need to have this code being copied into the core folder of each project anymore.

Here the original text:

One of the least talked about features of Angular 6 is Angular CLI Workspaces. Workspaces or Angular CLI Workspaces give angular developers the ability to have more than one project in one workspace. This brings all your projects under the same workspace. This is not limited to applications but also angular libraries.

This gives developers the freedom of breaking down large applications into smaller applications and modules. The modules such as authentication module can then be shared across the applications using those specific modules. This improves the workflow by reducing code repetition using shared libraries that are generated and managed by Angular CLI.

Great! Thanks for the answer, with this it makes sense.