Built-in library types in NX

What are buildable and publishable libraries

Project Source Code

Get the project source code below, and follow along with the lesson material.

Download Project Source Code

To set up the project on your local machine, please follow the directions provided in the README.md file. If you run into any issues with running the project source code, then feel free to reach out to the author in the course's Discord channel.

Table of Contents

This lesson preview is part of the Next-Level Angular Apps with NX course and can be unlocked immediately with a \newline Pro subscription or a single-time purchase. Already have access to this course? Log in here.

This video is available to students only
Unlock This Course

Get unlimited access to Next-Level Angular Apps with NX, plus 70+ \newline books, guides and courses with the \newline Pro subscription.

Thumbnail for the \newline course Next-Level Angular Apps with NX
  • [00:00 - 00:08] Let's talk about NX libraries for Angular applications. So we have three kinds of libraries and I want to discuss all of them in this lesson.

    [00:09 - 00:29] So I have a monorepo with the default Angular application generated and we will add a library. So as previously in our lesson, we need to execute Yarn NX generate at NX/Ang ular library and we need to add the name of our library.

    [00:30 - 00:45] So let's call it Angularlib and of course we need to set the prefix so - - prefix and app. Why not?

    [00:46 - 00:57] This kind of library we call the default library. As you can see, we have some files generated and you basically can add this library to your application or applications and use it.

    [00:58 - 01:06] It's very simple, you don't need to care about anything. What is important here is that it's not like npm library, you cannot publish it .

    [01:07 - 01:19] This kind of library will always be compiled as part of application. So when you have two applications and both of them use that library, it will be compiled twice with the application.

    [01:20 - 01:27] Of course it's a big disadvantage. You don't want to spend time compiling the same library over and over with every application you have.

    [01:28 - 01:37] The second kind of library you can have in NX/Monorepo is Buildable library. So to generate one, you need to execute command yarn NX generate.

    [01:38 - 01:56] It's the same as previously but you need to add additional flag - - buildable. And of course I need to change the name of library so Angular buildable library .

    [01:57 - 02:06] The first noticeable difference here is that you will have more files generated . You will have ng package JSON and package JSON.

    [02:07 - 02:19] Which means that you can build this library without building the whole application. And it is a huge difference because you can build it once and then use it multiple times just by linking it to your application.

    [02:20 - 02:30] And this kind of library - Buildable library is like a halfway step between the default one and publishable library. So let's generate the publishable library.

    [02:31 - 02:36] So we will execute the same command. So yarn NX generate Angular library.

    [02:37 - 02:56] We will change the library name to publishable. And you have to add one more flag so you have to leave that buildable flag and add - - publishable.

    [02:57 - 03:09] And then it is very important to add import path. So let's set the import path to my org/name of the library.

    [03:10 - 03:23] So this one here. So the main difference between publishable and buildable library is that publishable library is meant to be published in npm.

    [03:24 - 03:30] So we have three kinds of libraries. You have the default one and you need to compile it with your application.

    [03:31 - 03:40] You have buildable library and you may build it without the whole application and then reuse it. And you have library that is publishable.

    [03:41 - 03:52] It is both buildable and publishable so we can build it and then publish it. When you have at least buildable libraries it allows you to use the incremental build feature of NX.

    [03:53 - 04:11] It means that if some libraries haven't changed in your comment there is no need to build them because you have the previous artifacts and you can use cached packages instead of building it from the beginning. This method helps you save CPU time on your CI or local machine.

    [04:12 - 04:23] For small or medium applications build time is almost never a problem. But when you have application that is huge the build time may take maybe a minute, maybe a two minutes.

    [04:24 - 04:35] It depends. And if you can have the result the ng-serve comment or build on CI in short a time it's always better to have.

    [04:36 - 04:46] So if you ask me which one library type I prefer I would say that I prefer publishable libraries. Even if there is no way that you are going to publish it.

    [04:47 - 05:05] Having that idea behind your head that someday you may want to show it to other developers will force you to create better code. And it's way easier to not publish publishable library and just use it internally in your mono repo than trying to publish the default library.

    [05:06 - 05:06] It just less work.