Logo

dev-resources.site

for different kinds of informations.

How to Create a Library Package from an existing Angular App

Published at
6/15/2024
Categories
angular
libraries
npm
tutorial
Author
jcarloscandela
Categories
4 categories in total
angular
open
libraries
open
npm
open
tutorial
open
Author
14 person written this
jcarloscandela
open
How to Create a Library Package from an existing Angular App

Creating a library package from an existing Angular application can significantly streamline code reuse and modularity across different projects. In this guide, we'll walk through the process using the ng-packagr tool, with an example based on the Ionic Conference App.

Prerequisites

Before we begin, ensure you have an existing Angular project. For this example, we'll use the Ionic Conference App.

  1. Clone the Ionic Conference App:
   git clone https://github.com/ionic-team/ionic-conference-app
   cd ionic-conference-app
Enter fullscreen mode Exit fullscreen mode

Step 1: Install ng-packagr

First, install ng-packagr in your project:

npm install ng-packagr
Enter fullscreen mode Exit fullscreen mode

Step 2: Create ng-package.json

Next, create a ng-package.json file in the root of your project with the following content:

{
    "$schema": "./node_modules/ng-packagr/ng-package.schema.json",
    "allowedNonPeerDependencies": [
        "@angular/common",
        "@angular/core",
        "@angular/forms",
        "@angular/platform-browser",
        "@angular/router",
        "@angular/service-worker",
        "@awesome-cordova-plugins/core",
        "@awesome-cordova-plugins/in-app-browser",
        "@capacitor/android",
        "@capacitor/app",
        "@capacitor/core",
        "@capacitor/device",
        "@capacitor/haptics",
        "@capacitor/ios",
        "@capacitor/keyboard",
        "@capacitor/splash-screen",
        "@capacitor/status-bar",
        "@ionic/angular",
        "@ionic/storage-angular",
        "cordova-plugin-inappbrowser",
        "core-js",
        "rxjs",
        "sw-toolbox",
        "wait-on",
        "webdriver-manager",
        "zone.js"
    ]
}
Enter fullscreen mode Exit fullscreen mode

This configuration file specifies the dependencies that are allowed in the package. The app depends on those packages, so we need to add them inside allowedNonPeerDependencies.

Step 3: Create a Public API File

Create a public_api.ts file in the src folder to export the components and modules you want to make available for import in other projects. For example:

export * from './app/shared/shared.component';
export * from './app/shared/shared.module';
Enter fullscreen mode Exit fullscreen mode

Step 4: Define Components and Modules

Define the components and modules you want to share. Here’s an example of a shared component and module:

shared.component.ts

import { Component } from '@angular/core';

@Component({
  selector: 'app-shared',
  template: `
    <div>
      <h2>Shared Component</h2>
      <p>This is a shared component created for reuse.</p>
    </div>
  `
})
export class SharedComponent { }
Enter fullscreen mode Exit fullscreen mode

shared.module.ts

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { SharedComponent } from './shared.component';

@NgModule({
  declarations: [
    SharedComponent
  ],
  imports: [
    CommonModule
  ],
  exports: [
    SharedComponent
  ]
})
export class SharedModule { }
Enter fullscreen mode Exit fullscreen mode

Step 5: Update package.json Scripts

Add the following script to your package.json:

"scripts": {
  "package": "ng-packagr -p ng-package.json"
}
Enter fullscreen mode Exit fullscreen mode

Run this script to generate the package in the dist folder:

npm run package
Enter fullscreen mode Exit fullscreen mode

Step 6: Link the Package Locally

Once the package is generated, navigate to the dist folder and create a link to reuse the package locally:

cd dist
npm link
Enter fullscreen mode Exit fullscreen mode

Step 7: Consume the Library in Another Project

In the project where you want to use the library, run the following command to link the library:

npm link ionic-conference-app
Enter fullscreen mode Exit fullscreen mode

Import the necessary components and modules from the library and use them in your project. If you encounter build errors during execution, add the following to angular.json to resolve symlink issues:

"architect": {
  "build": {
    "options": {
      "preserveSymlinks": true
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

Step 8: Make Changes and Rebuild the Package

If you need to make changes to your library package, update the code as needed and then run the packaging script again to generate the updated package:

  1. Make your changes to the components, modules, or any other part of the library.
  2. Run the packaging script to rebuild the package:
   npm run package
Enter fullscreen mode Exit fullscreen mode

Conclusion

By following these steps, you can efficiently create a library package from an existing Angular application and reuse components and modules across different projects. This approach not only promotes code reuse but also simplifies maintenance and updates. Happy coding!

libraries Article's
30 articles in total
Favicon
Top 5 Python Libraries to Watch in 2025
Favicon
The Use of TeeChart Charting Libraries in EMD International’s Renewable Energy Solutions
Favicon
Common Java Libraries and Frameworks you Should Try
Favicon
TeeChart Charting Libraries use cases
Favicon
Scientific problems are not real problems for programmers
Favicon
Top 8 AI Open Source Software Libraries
Favicon
How to Create a Library Package from an existing Angular App
Favicon
New in ngx-errors 4.0
Favicon
List of awesome CSS frameworks, libraries and software
Favicon
NPM libraries to build your next AI projects
Favicon
How to use external libraries in Theme App Extensions for your Shopify App
Favicon
What are headless UI libraries?
Favicon
Best Javascript Machine Learning Libraries in 2024
Favicon
5 C# Word Libraries Most .NET Developers Use in Project
Favicon
C# PDF Libraries Compared for .NET Developers: Pros & Cons
Favicon
Essential AI Tools and Libraries: A Guide to Python, Git, C++ Compile Tools, FFmpeg, CUDA, PyTorch
Favicon
8 Python Libraries You Might Not Be Using But Should
Favicon
documented: make docstrings in your exceptions work
Favicon
32 Best Passkey Libraries
Favicon
Can I access the JavaScript native Math library source code?
Favicon
What’s the difference between a library and a framework?
Favicon
Comparing React Testing Libraries
Favicon
Exploring CDN Links for CanvasJS Charts and Stockcharts
Favicon
Backup manually installed libraries and packages in Ubuntu
Favicon
What is your favorite web development tool or framework, and what makes it valuable to you?
Favicon
Only internally vetted and approved Open Source libraries can be used
Favicon
Top Libraries Used for React JS Rendering
Favicon
A Brief Overdrive Library Analysis
Favicon
Top 10 technologies/framework to learn as a MERN stack developer in 2023
Favicon
Why is State Management Important For React Apps?

Featured ones: