Angular Academy · Lesson

Creating a Library with ng generate

Scaffold a library project.

Lesson 1 of 413 steps

Creating a Library with ng generate is a free Angular Academy lesson on CoddyKit. This is lesson 1 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Angular Academy learning path, and your progress syncs across the web and the CoddyKit app. The Angular Academy course includes 4 lessons in total.

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.

Free to start

Learn HTML with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
38
Lessons
144

Frequently Asked Questions

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

Yes — any 3 lessons of the Angular Academy learning path, including “Creating a Library with ng generate”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Angular Academy course includes 4 lessons in total.

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, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.

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