0Pricing
Angular Academy · Lesson

Creating a Library with ng generate

Scaffold a library project.

Creating a Library with ng generate is a free Angular Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Angular Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Why Build a Library

When several apps need the same components, services, or utilities, you package them as an Angular library. A library is reusable, versioned, and publishable, instead of copy-pasting code between projects.

Workspaces Hold Libraries

Libraries live inside an Angular workspace: a folder managed by one angular.json that can contain multiple apps and libraries. You can add a library to an existing workspace or create a dedicated one.

ng new my-workspace --create-application=false
# a workspace with no app, ready to hold libraries

Generating a Library

The schematic ng generate library (alias ng g lib) scaffolds a new library project under projects/.

ng generate library ui-kit
# creates projects/ui-kit with a sample component, module, and public-api.ts

What Gets Created

The library comes with its own src/lib folder, a public-api.ts barrel, a ng-package.json (for the build), and an entry in angular.json. It is a self-contained, buildable project.

projects/ui-kit/
  src/
    lib/        # your components & services
    public-api.ts
  ng-package.json
  package.json

Adding Components to the Library

Generate code into the library by targeting it with --project. The CLI places files in the right folder.

ng generate component button --project=ui-kit
ng generate service theme --project=ui-kit

Standalone Components in Libraries

Modern libraries favor standalone components, which consumers import directly without an NgModule. Mark the component standalone: true so apps can use it with a simple import.

@Component({
  selector: 'lib-button',
  standalone: true,
  template: '<button class="lib-btn"><ng-content></ng-content></button>',
})
export class ButtonComponent {}

The Library package.json

Each library has its own package.json defining its name, version, and peer dependencies like @angular/core. Peer deps mean the consuming app supplies Angular, avoiding duplicate copies.

{
  "name": "ui-kit",
  "version": "0.0.1",
  "peerDependencies": { "@angular/core": "^18.0.0" }
}

Developing Against a Local App

Keep a demo app in the same workspace to exercise the library while you build it. Importing by the library name resolves to the local source via TypeScript path mappings in tsconfig.

Library vs Application

An application is meant to be deployed; a library is meant to be consumed. Libraries cannot be served directly, they are built into a distributable package and either published or imported by apps in the workspace.

Keep Libraries Focused

A good library has a clear purpose (a UI kit, a data-access layer). Avoid dumping unrelated code together; cohesive libraries are easier to version, document, and reuse.

Next Steps

With the library scaffolded, the remaining work is defining its public API, building it into the Angular Package Format, and publishing it. The next lessons cover each.

Quick Check

How do you create a library?

Recap

Libraries package reusable code for many apps. Create one with ng generate library inside a workspace; it scaffolds src/lib, public-api.ts, ng-package.json, and a package.json with peer dependencies. Add code via --project, prefer standalone components, and keep each library focused.

Frequently asked questions

Is the “Creating a Library with ng generate” lesson free?

Yes — the full text of “Creating a Library with ng generate” is free to read here on the web, and the Angular Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Angular Academy course, upgrade to CoddyKit PRO.

What will I learn in “Creating a Library with ng generate”?

Scaffold a library project. You practise Angular Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Angular Academy?

No prior experience is required. Angular Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Creating a Library with ng generate” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Angular Academy lesson?

Yes. Every Angular Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Creating a Library with ng generate
  2. Public API and Entry Points
  3. Building with ng-packagr
  4. Publishing to npm
← Back to Angular Academy