How to Build and Publish Angular Modules
First, never import BrowserModule
. Your module is a feature module, only the final user should import BrowserModule, in the app root module. If you need the common directives (*ngIf, *ngFor…), import CommonModule
.
If your module is about creating new components, directives or pipes, do not forget to export them. Declared ones are only accessible inside your module.
Respect the Angular rule: never use browser-specific APIs (like the DOM) directly. If you do so, your module won’t be compatible with Angular Universal(SSR) and other Angular advanced options. If you really need to use browser-specific APIs (localStorage…), you should try/catch errors.
Most importantly, do not mix components/directives/pipes and services in the same module. Why?
A service provided in a module that will be available everywhere in the app, so your module should be imported only once, in the user app root module (like the Http module).
An exported component/directive/pipe will only be available in the module importing yours, so your module should be imported in every user module (root and/or feature modules) that need them (like the CommonModule).
Don't use a name like @my/module
those are scoped modules currently only unscoped, primary entry-point libraries are supported.
Good article to go deep about ng modules and its scopes: https://medium.com/@cyrilletuzi/understanding-angular-modules-ngmodule-and-their-scopes-81e4ed6f7407
Finally publishing NPM Packages: https://docs.npmjs.com/getting-started/publishing-npm-packages