our

Latest Blogs

How to build and publish Angular library

iFour Team -June 11, 2021

Listening is fun too.

Straighten your back and cherish with coffee - PLAY !

  •  
  •  
  •  
How to build and publish Angular library

What is the Angular library?

The Angular library is a collection of components, services, modules, etc. used to share across different angular projects. The angular library must be important and used in an angular application.

Why we use the Angular library?

Many times components, services, and directives, etc. could be applied to multiple angular projects. So we can use this code in a different application. This angular library can be used in your local work area and you can publish as npm packages to share with another angular project or with other angular applications across the globe.

Example of how to build and publish Angular library

Step 1:

With Angular CLI, it is easy to manage the angular library. To create a library, we have the simple command below:

It is the updated angular.json, package.json, and tsconfig.json files of our main angular application. Now, we will check that the library is working properly in our local angular project or not and if it works properly then you can publish it to npm.

Step 2:

Now open this angular app in solution explorer, you can see it has a new generated library my-library under the project folder.

In package.json and other files have their dependencies and version.

In the public-api.ts file, this is the entry point for our library project.

And already generated the ‘my-library’ component. Inside the my-library.component.ts file, it has a template in the component decorator.

Now we use the library.

To use this library anywhere first we need to build the library. You cannot use a library without building a disc folder. Write the below command in your terminal:

ng build

This command will automatically create a disc folder, the compiled library is stored. You can use this library in any angular application.

Step 3:

Now, in solution explorer open the app.module.ts file and import our library just like we import any other in-built library of angular.

Now, we can use our library component on our app component.

In app.component.html file we add below code:

Now you run your angular application using ng serve --open command in your terminal.

output of the component

[output of the component]

Step 4:

Now we add more functionality to our library project.

We add one simple component to having counter functionality.

ng g c counterdemo --project my-library

The my-library.module.ts file automatically updated. But in this file, it has only declared counter demo component and if we want to use this counter demo component outside in any angular application, and we must have to add it to the exports array inside the module decorator.

One-stop-bestemming voor AngularJS-ontwikkeling informeer vandaag nog.

And also do not forget to export it in to the public-api.ts file also.

In counterdemo.component.html, and we will have two buttons one for the increment the counter value and another one is for the decrement the counter value by clicking on it.

counetrdemo.component.html file

counetrdemo.component.ts file

Now, use the counterdemo component in our angular project. For this, we have to create the rebuild.

ng build my-library

app.component.html file

Now you can run your project.

Step 5:

Now we will publish the library to local npm. For this, first, we build to this library, and then we can publish it into the local node package manager registry so we can use this library to any angular project.

This will pack a file with the name my-library-0.0.1.tgz.

Our library is publishing has done now we can use this library in any angular application.

We use this library in another project using the below command:

npm install C:angularlibrarydistmy-librarymy-library-0.0.1.tgz

Conclusion

In this blog, we have seen what is the angular library and how to build and publish angular library. We can use angular library locally in the workspace, or you can publish as npm packages to share with other projects and with other angular developers globally. You can use the angular library in any angular project.

Work with us

Well do everything we can to make our next best project!

Our Insights

Check out our most recent blogs

An in-depth guide on Angular Dependency Providers
An in-depth guide on Angular Dependency Providers

What is Angular? Angular is a frontend development framework used for building single-page client applications using HTML and Typescript. It is written in Typescript. What...

A simple guide on AOT Compilation in Angular
A simple guide on AOT Compilation in Angular

What is a Compiler? A compiler is nothing but a part of code that converts one programming language to another. If we talk about some simple programming languages like C, C++,...

A simple guide on NgRx Entity in Angular
A simple guide on NgRx Entity in Angular

Introduction Angular is a remarkable framework that may be used to create mobile and desktop web apps with spectacular UIs. It's made with JavaScript. we can utilize HTML, CSS,...

Our Partners

Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo

Work With Us

Well do everything we can to make our next best project!