← All posts

How to Create NPM Library Using Existing Angular Code

Angular libraries provide a powerful mechanism for code sharing and modular development in Angular applications. By encapsulating reusable components, services, and other features into libraries, developers can streamline their projects, encourage best practices, and facilitate collaboration. In this guide, we’ll walk through the process of creating an Angular library, from setup to publishing.

Prerequisites

Before diving into library creation, ensure that you have a registered npm account and a generated Angular project with peace of code for the library.

Existed example for the library

For instance, I have code that simulates terminal text typing:

So our goal is to add a typing component to npm for use as a third-party library for any project like this:

<terminal-text-typing [text]="'SUPER TEXT'" [speed]="1000" />

Setup

First of all, we need to open the existing project:

cd your-project-name

Or create a new one:

ng new your-project-name
cd your-project-name

Create a Library

Be careful with your library naming, because it is very important for publishing. Look at the Angular naming best practices:

Avoid using a name that is prefixed with ng-, such as ng-library. The ng- prefix is a reserved keyword used from the Angular framework and its libraries. The ngx- prefix is preferred as a convention used to denote that the library is suitable for use with Angular. It is also an excellent indication to consumers of the registry to differentiate between libraries of different JavaScript frameworks.

Inside your Angular workspace, use the Angular CLI to generate a new library:

ng generate library your-library-name

This command creates a new folder projectswith theyour-library folder inside it with the necessary structure for an Angular library:

It also changes your angular.json file:

Develop Your Library

Navigate to the library’s project directory:

cd projects/your-library/

This directory is where you’ll develop your library’s code. Create components, services, or any other features you want to include in your library.

You can copy your existing code components into the lib folder:

<!-- angular-terminal-text-typing.component.html -->

<div class="type-effect-container">

<span class="text" [innerText]="currentText"></span>
<span [ngClass]="terminalCursorClass"></span>

</div>
// angular-terminal-text-typing.component.ts

import {Component, EventEmitter, Input, OnInit, Output} from '@angular/core';
import { CommonModule } from '@angular/common';

@Component({
selector: 'terminal-text-typing',
standalone: true,
imports: [CommonModule],
templateUrl: './angular-terminal-text-typing.component.html',
styleUrls: ['./angular-terminal-text-typing.component.scss']
})
export class AngularTerminalTextTypingComponent implements OnInit{
@Input() text!: string;
currentText = '';
@Input() public speed: number = 55; // Adjust the typing speed (milliseconds)
private lineBreakDelay: number = 500; // Adjust the delay between line breaks (milliseconds)
public terminalCursorClass = 'terminal-cursor';
@Output() public onTypingEnd: EventEmitter<boolean> = new EventEmitter<boolean>();
async ngOnInit(){
await this.typeText();
}
async typeText() {
for (let i = 0; i < this.text.length; i++) {
this.currentText += this.text[i];
if (this.text[i] === '\n') {
await this.timeout(this.lineBreakDelay);
}
await this.timeout(this.speed);
}
this.terminalCursorClass = '';
this.onTypingEnd.emit(true);

}
async timeout(ms: number) {
return new Promise(resolve => setTimeout(resolve, ms));
}

}

Note: You should remember that the selector name (in my code — terminal-text-typing) will be used in your npm package.

Build Your Library

Build your library using the Angular CLI:

ng build your-library

This command compiles your library and generates distributable files inside the dist/your-library directory:

Publish Your Library

If you wish to share your library with the community, you can publish it to npm. First, make sure you have an npm account and are logged in:

npm login

Navigate to the library’s dist directory:

cd dist/your-library

Publish your library to npm:

npm publish

Note: If you want to make changes in your library and publish it again you need to change the version in the library’s package.json file, build and publish it again:

ng build your-library
cd dist/your-library
npm publish

If you follow all these steps you can find your new library in your npm account:

https://www.npmjs.com/package/angular-terminal-text-typing

Consuming Your Library

In another Angular project, you can install and use your library:

npm i angular-terminal-text-typing

Import the component into your application:

import { Component } from '@angular/core';
import {AngularTerminalTextTypingComponent} from "angular-terminal-text-typing";

@Component({
selector: 'app-root',
standalone: true,
imports: [AngularTerminalTextTypingComponent],
templateUrl: './app.component.html',
styleUrl: './app.component.scss'
})
export class AppComponent {}

Add selector to HTML template:

<terminal-text-typing [text]="'SUPER AWESOME TEXT'" [speed]="1000" />

Documentation:

  1. The full source of this example you can find on my GitHub
  2. Angular documentation
  3. npm documentation

By following these steps, you can contribute to the Angular ecosystem by sharing your reusable components and services with the community.

And there we have it. I hope you have found this useful. Thank you for reading. If you have any questions you can find me on LinkedIn. You can also read my previous articles: