Publish Angular Library to NPM

manthanank

Manthan Ankolekar

Posted on January 22, 2023

Publish Angular Library to NPM

Install Angular cli to create Angular project

npm install -g @angular/cli
Enter fullscreen mode Exit fullscreen mode

Run the cli command to create new Angular Project

ng new test-library-angular
Enter fullscreen mode Exit fullscreen mode

Navigate to the project

cd test-library-angular
Enter fullscreen mode Exit fullscreen mode

Next generate library using cli command

ng generate library ng-library-log
Enter fullscreen mode Exit fullscreen mode

Import NgLibraryLogModule into app.module.ts

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { NgLibraryLogModule } from 'projects/ng-library-log/src/public-api';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    NgLibraryLogModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }
Enter fullscreen mode Exit fullscreen mode

Update package.json from dist/ng-library-log as shown below

{
  "name": "ng-library-log",
  "version": "0.0.1",
  "author": {
    "name": "Manthan Ank"
  },
  "keywords": [
    "angular",
    "javascript"
  ],
  "repository": {
    "type": "git",
    "url": "https://github.com/manthanank/test-libary-angular.git"
  },
  "description": "Test Library in angular",
  "peerDependencies": {
    "@angular/common": "^15.1.0",
    "@angular/core": "^15.1.0"
  },
  "dependencies": {
    "tslib": "^2.3.0"
  },
  "sideEffects": false,
  "module": "fesm2015/ng-library-log.mjs",
  "es2020": "fesm2020/ng-library-log.mjs",
  "esm2020": "esm2020/ng-library-log.mjs",
  "fesm2020": "fesm2020/ng-library-log.mjs",
  "fesm2015": "fesm2015/ng-library-log.mjs",
  "typings": "index.d.ts",
  "exports": {
    "./package.json": {
      "default": "./package.json"
    },
    ".": {
      "types": "./index.d.ts",
      "esm2020": "./esm2020/ng-library-log.mjs",
      "es2020": "./fesm2020/ng-library-log.mjs",
      "es2015": "./fesm2015/ng-library-log.mjs",
      "node": "./fesm2015/ng-library-log.mjs",
      "default": "./fesm2020/ng-library-log.mjs"
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

Build the library generated

ng build ng-library-log
Enter fullscreen mode Exit fullscreen mode

Navigate to the folder built

cd dist/ng-library-log
Enter fullscreen mode Exit fullscreen mode

To publish to NPM enter the command mentioned below

npm publish
Enter fullscreen mode Exit fullscreen mode

Structure of Project

project-structure

Link to Package: ng-library-log

💖 💪 🙅 🚩
manthanank
Manthan Ankolekar

Posted on January 22, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Publish Angular Library to NPM
angular Publish Angular Library to NPM

January 22, 2023

Using npm link in Angular9+
angular Using npm link in Angular9+

January 5, 2021