To create an Angular library, generate it inside an Angular workspace with ng generate library my-lib, build it with ng build my-lib, and, if you want it on npm, publish the generated dist/my-lib folder after a production build. The steps below follow Angular’s current library documentation, checked in October 2026. Command defaults and recommendations can change between Angular major versions, so confirm them against the documentation for your version before you ship.
Decide whether the code deserves a library
An Angular library is an Angular project that differs from an application in that it cannot run on its own. It has to be imported by an application. A library can stay inside a single workspace, or it can be published as an npm package that other projects install (Angular, “Libraries • Overview”).
As an Amazon Associate I earn from qualifying purchases.
Packaging code is an architectural decision, not just a file move. A separate package enforces a boundary between reusable feature code and your application’s business logic. In return, you take on extra design work, maintenance, and update work. Create a library when a feature is genuinely reusable across applications and you can name those applications now. If the code is only shared by one app, keeping it in the app’s folder is usually simpler.
PC Slower Than It Used to Be?
A free scan shows the junk files, broken settings and background clutter dragging Windows down - then fixes them in one click.Free scan · Windows 10 & 11Outdated Drivers Are Slowing You Down
One free scan finds every outdated or missing driver and matches the right update for your exact hardware.Free scan · exact hardware matchGenerate the library
Angular’s documented sequence for a workspace dedicated to holding libraries is:
#1 Best Overall
- Create a workspace without an application:
ng new my-workspace --no-create-application - Move into the workspace:
cd my-workspace - Generate the library:
ng generate library my-lib
The CLI creates the library under projects/my-lib and registers a library project in angular.json. The generation reference documents ng generate library, which also works as ng generate lib, along with options for the public API entry file and the component selector prefix (Angular CLI, “library” generation reference). New projects added to a workspace default to a projects/ subfolder.
You can also generate a library inside an existing application workspace. Workspace commands such as ng generate must run from inside the workspace folder, so change into it first (Angular CLI, local setup).
Define the public API and entry points
The public-api.ts file controls exactly what consumers can import. Export your supported components, services, and utilities through that file, and do not encourage consumers to import internal files directly. Angular also recommends a README that covers installation and maintenance (Angular, “Creating libraries”).
Quick wins for a faster PC:
Scan for outdated or missing drivers - takes under a minuteDriver Scan →Clear out junk files and repair common Windows errorsFree Scan →Primary entry point
Every library has one primary entry point. Its public API defines the package’s root import path, such as my-lib, so the exports you list there are what applications see by default.
Rank #2
Secondary entry points
Additional secondary entry points give consumers structured import paths, such as my-lib/button. Each secondary entry point has its own ng-package.json and its own public API. The packager discovers these entry points during the build.
Two rules keep entry points stable:
- Import across entry points with the package import path (for example,
my-lib/button), never with a relative path into another entry point’s source. Entry points build separately, so relative imports can break the build. - Avoid circular dependencies between entry points. They can cause build failures.
Secondary entry points are useful when a library has several clearly separate areas. For a small library with a handful of exports, one primary entry point is easier to maintain.
Build and use the library locally
Build the library before you import it into an application in the same workspace:
ng build my-lib
The CLI sets up TypeScript path mappings so that applications resolve the library to its built output, not to its source TypeScript. Keep it that way. The library and the application can process TypeScript differently, and importing source files can produce errors that do not appear in the library’s own build.
Rank #3
During development, run the build in watch mode to rebuild incrementally when files change:
ng build my-lib --watch
Library packages are built with ng-packagr, which Angular CLI uses for library builds (Angular, build documentation). This is a different pipeline from the one that builds applications, so do not assume application build settings or behavior carry over. Check library-specific options in the library build reference rather than copying them from your app’s configuration.
Prepare and publish to npm
If the library is only used inside one workspace, a local build is enough and you can skip this section. For npm distribution, build with the production configuration so the output is optimized and in the correct package format, then publish from the generated distribution folder:
Free tools Windows power users keep installed
One-click scans. No signup required.
- Run the production build:
ng build my-libThe default build for a library is the production configuration, which is the one Angular recommends for distribution.
- Move into the output folder:
cd dist/my-lib - Publish the package:
npm publish
Publish from dist/my-lib, not from the workspace root. The root contains source code and configuration that consumers do not need.
Peer dependencies on Angular
Library packages list @angular/* packages as peer dependencies, not regular dependencies. This ensures that the application and the library share one Angular module instance. If the library bundled its own copy of Angular, the application could end up with two copies, and dependency injection and change detection could fail in confusing ways.
Output format and version compatibility
Angular recommends partial-Ivy output for packages published to npm. Its portable form can be consumed by applications that use Angular v12 or later. The table below compares the two options that Angular documents.
Do these 3 things before closing this tab:
1Scan for outdated or missing drivers - takes under a minute2Clear out junk files and repair common Windows errors3Fix the driver behind crashes, sound loss and screen glitches| Output form | Angular recommendation for npm | What a consuming application needs |
|---|---|---|
| Partial-Ivy | Recommended for public npm packages | Angular v12 or later. The application should use the same or a newer Angular version than the library. |
| Full-Ivy | Advised against for npm publication | Relies on private instructions, so it requires matching Angular versions between library and application. |
In practice, this means a library built against Angular 18 can be used by an application on Angular 18 or any newer version, but not by an application that is older than the library’s Angular version (Angular, npm package reference). Version numbers in this example are illustrative; check the npm package reference for the exact compatibility rules for your release.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Add schematics only when consumers benefit
A library can ship schematics that plug into Angular CLI commands such as ng add. They are optional. Add them when consumers gain something from guided setup or code generation, for example a schematic that configures a feature in the consumer’s project or scaffolds files. A library without schematics installs and works the same way (Angular CLI, “ng add”; Angular, library schematics).
Choose between workspace reuse and publishing
| Factor | Workspace-only reuse | npm publication |
|---|---|---|
| Distribution scope | Applications in the same workspace | Any project that installs the package |
| Build required | Yes, ng build my-lib before consuming it |
Yes, a production build of the distribution folder |
| Release and versioning | Not needed; the library changes with the workspace | Required; you own versions, changelogs, and compatibility |
| Consumer installation | Not applicable; consumed through path mappings | Consumers install the package independently |
| Angular compatibility | Same workspace Angular version | Consumers must use the same or a newer Angular version (partial-Ivy output) |
Start with workspace reuse. It is cheaper to maintain, and you can publish later when other teams or projects need the same code.
Checklist before you publish
- The library builds with the production configuration and is published from
dist/my-lib. - Only the intended components, services, and utilities are exported from
public-api.ts. - Every secondary entry point has its own
ng-package.jsonand public API, and cross-entry-point imports use package import paths. @angular/*packages are peer dependencies.- The README explains installation and maintenance.
Once the checklist passes, publish with npm publish and test the package in a clean application that uses the same Angular version or a newer one.
Quick Recap
The Bottom Line
“”
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




