Home > Blockchain >  Do I need to create an Angular library to publish a simple TS class that references Angular classes
Do I need to create an Angular library to publish a simple TS class that references Angular classes

Time:06-14

I have an existing Typescript class named APIResponse used in one Angular project that I would like to make available as an NPM package so that I can use it other projects. The class is not an Angular artifact (Service, Component, etc.). I think I only need to create a "standard" Typescript package, rather than generating an Angular library, and this question seems to support that.

However, the APIResponse class does reference the Angular class HttpErrorResponse from @angular/common/http.

Does referencing this class mean that I need to create an Angular library, or do I just need to add a dependency to an Angular package in package.json? And if so, which one?

This is the first time I've tried to publish an NPM package, so I'm still learning some of the language and terminology associated with this process.

CodePudding user response:

No, you can publish normal TypeScript packages to NPM registry without Angular. This short medium blog can be helpful for your first Ts package.

As far as Angular is concerned in the context of your question, yes I think you need to use @Angular/common to take advantage of HttpErrorResponse Which is part of Angular HttpClientModule which is part of @Angular/common

There is a tendency to avoid publishing to NPM registry unless you need to. This is because of many pitfalls (versioning related problems).

I am not sure if you heard of Nx orchestration system for monorepos. It is an awesome tool that helps big as well as small and medium sized projects to manage shared logic, UI components, and utilities (your case) between multiple apps (even from different frameworks say same Interface or Class shared between Angular and Vue).

So the same concept of NPM libs but locally shared between different projects in the same repository (you can also publish them via nx). It is super fast, clean and powerful tool to handle such issues. I have tried it and it is incredible when it comes to reusability.

  • Related