October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PCOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk5 min

How to Create and Publish an Angular Library

Create an Angular library with ng generate library, build it, and publish the dist folder to npm with Angular's recommended partial-Ivy output and peer dependencies.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Generate the library

Angular’s documented sequence for a workspace dedicated to holding libraries is:

  1. Create a workspace without an application:
    ng new my-workspace --no-create-application
  2. Move into the workspace:
    cd my-workspace
  3. 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”).

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

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:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
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.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  1. Run the production build:
    ng build my-lib

    The default build for a library is the production configuration, which is the one Angular recommends for distribution.

  2. Move into the output folder:
    cd dist/my-lib
  3. 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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
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.Support on Ko-Fi

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.json and 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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.