Language:

Search

[Advance 1]: Creating Library with Angular

  • Share this:
post-title

Create Empty Angular Project

 

First of all, we need to create empty angular project by following command line. Note that we are developing from scratch. So, please start with appropriate library name which can be used in your angular project or published to npm.

ng new testlib --create-application=false

When you run above command line, it will create empty angular application. It means, there is no app folder created inside the angular project. Here, the flag --create-application=false is the reason for empty angular project.

2022-01-20 17_29_21
 

Create Our Library in Angular Project

 

The next step is our main thing. We need to create library for our own angular library creation. For this, open the newly created project with your favourite editor. Here, I m using VS Code. When you open the testlib project in VS Code, you will have below in your project.

2022-01-20 17_38_18

This is time to create the library. We can create the new library by following command line
 

ng g library ucme --prefix=my

You can see the new folder created in your project. It look like our normal angular application folder structure. Yes, it will also have modules, component which is created while we create library. You may remove those default files inside the lib folder. The above command line will create below modfication in our project.

2022-01-20 17_56_06

We deleted default files from lib folder except component and module files. The lib folder look like below.

2022-01-20 22_12_25
 

After that, we modified the component file with below (please remove space between < and div> and also in between < and ./div>),

import { Component, Input, OnInit } from '@angular/core';

@Component({
  selector: 'my-ucme',
  template: `
    < div>{{myData}}< /div>
  `,
  styles: [
  ]
})
export class UcmeComponent implements OnInit {
  myData:any;
  @Input("dataInput") dataInput: string='';
  constructor() { }


  ngOnInit(): void {
    this.myData=this.dataInput.toUpperCase();
  }
}

and also we modified the public-api.ts like below

2022-01-20 20_59_40
 

Create Application in Angular Project

 

Our library is now ready. Now we want to test our library with application. For this, we need to create application in our project.

 

ng g application testapp

 

This will create additional folder inside the projects folder look like as below.

2022-01-20 19_11_11
 

To test our library, we need to modify the app.module.ts and app.component.html. Follow the below modifications,

app.module.ts

2022-01-21 00_05_30
 

app.component.html

2022-01-21 00_06_52
 

To test our library

 

We are now ready to test our library. Run below command line,

 

ng serve -o

 

If you get below result, then our library is working fine without any issue.

2022-01-20 19_51_34
 

Packaging our library

 

We can package our library either in local or npmjs. First we look into building our library and then move into packaging.

By using below command line, we can build our library. We should use library name followed by the flag "--project".

 

ng build --project=ucme

 

Once we executed the above, we will get below result and below new folder in our project.

2022-01-21 10_55_35
2022-01-21 10_59_56
 

Now, our library ready to package. If we want to local build, then we can use below command line. For packaging, please make sure you are in library folder inside dist folder.

> cd dist/ucme

> npm pack

Once our package ready, you can see ucme-0.0.1.tgz file inside the dist/ucme.

2022-01-21 11_05_45
2022-01-21 11_22_07
 

With the ucme-0.0.1.tgz, you can install locally.

Now we are going to publish build library into npm. For this, you need to login NPM in command line with below command. You may ask to enter verification code after enter user name and password.

 

npm login

 

Once we logged in successfully into NPM in commandline, now we need to do some changes on package.json inside dist/ucme. We have modified the package.json below.

2022-01-21 11_40_04
 

Here, @siva7170/ucme is package name but scoped one. Because the name is started with @. The siva7170 is the username of NPM.

But, we will face one issue when we push into NPM. We use usually below command to push into NPM

 

npm publish

 

2022-01-21 11_43_23
 

We got the above error. Because, when we publish scope package to NPM, then our account should be premium account or we should publish our package into NPM with publish access.

To overcome this issue, we use below command line when we would like to publish with public access.

 

npm publish --access=public

 

2022-01-21 11_46_51
 

Now, we are ready to use in our angular project.

 

Use in our Angular Project

 

With ucme-0.0.1.tgz,

 

npm install < folder path >/ucme-0.0.1.tgz

 

With NPM,

 

npm install @siva7170/ucme

 

 

Siva P SV

Siva P SV

I am developer and blogger. I am experienced in PHP, .NET, Android, Ionic, I know these kind of language. Because, I would like to learn new things in this world. I am still learning much more things. Oh, I forgot to mention that I am also one of the Co-Founders of Tuty Rocks