Sub topic 16
Multi-project workspaces and libraries
This chapter assumes you can read angular.json, run ng generate and ng build, and know what a standalone component is. Earlier chapters covered those. Here you will see how one workspace can hold several applications and libraries, how they find each other, and where this setup tends to break.
Single-project and multi-project workspaces
A workspace created with ng new my-app holds one application. Its source sits in src/ at the root, and angular.json has one entry under projects with "root": "".
A multi-project workspace holds several projects. Each has its own projects/<name>/ folder and its own entry in angular.json. A project is either an application or a library, and the projectType field says which.
| Aspect | Single-project workspace | Multi-project workspace |
|---|---|---|
| Source location | `src/` at the root | `projects/<name>/src/` per project |
| `projects` entries in `angular.json` | One | One per application or library |
| Dependencies | One `package.json` | One shared root `package.json` |
| Project selection | Implicit | Pass the project name, or run from inside its folder |
| Typical use | One deployable app | Several apps sharing code, or a package to publish |
All projects share the root node_modules, package.json and root tsconfig.json. That keeps versions consistent, but it also means every project uses the same Angular version.
Starting a multi-project workspace
You can add projects to an existing workspace. To start clean, create a workspace with no application:
ng new company --no-create-application
cd company
ng generate application storefront
ng generate application admin
ng generate library shared-uiThe result looks like this:
company/
angular.json
package.json
tsconfig.json
projects/
storefront/ (application: src/main.ts, tsconfig.app.json, ...)
admin/ (application)
shared-ui/ (library: src/public-api.ts, ng-package.json, ...)Each application folder has its own src/main.ts, index.html and tsconfig.app.json. Each project entry in angular.json uses projects/<name> as its root and projects/<name>/src as its sourceRoot.
To work on one application, name it:
ng serve admin
ng test shared-ui
ng build storefront --configuration developmentThe CLI can also infer the project from your current directory. Passing the name explicitly is clearer in scripts and CI.
What a library contains
ng generate library shared-ui creates these files:
src/lib/holds the library code. The generator adds a sample component and service, and the exact sample varies by CLI version.src/public-api.tslists everything the library exposes.ng-package.jsonconfiguresng-packagr, the tool that packages the library. It sets the output folder and the entry file.package.jsonis the package manifest that will be published. It holds the name, version andpeerDependencies.tsconfig.lib.json,tsconfig.lib.prod.jsonandtsconfig.spec.jsonhold the TypeScript settings for building and testing.
The library's build target in angular.json uses an ng-packagr builder instead of the application builder:
{
"projects": {
"shared-ui": {
"projectType": "library",
"root": "projects/shared-ui",
"sourceRoot": "projects/shared-ui/src",
"prefix": "lib",
"architect": {
"build": {
"builder": "@angular-devkit/build-angular:ng-packagr",
"options": {
"project": "projects/shared-ui/ng-package.json"
},
"configurations": {
"production": { "tsConfig": "projects/shared-ui/tsconfig.lib.prod.json" },
"development": { "tsConfig": "projects/shared-ui/tsconfig.lib.json" }
},
"defaultConfiguration": "production"
}
}
}
}
}The builder package name depends on your CLI version. Newer releases use @angular/build:ng-packagr. Check what your own angular.json contains before copying anything.
The prefix is lib by default, so generated selectors look like lib-badge. Pass --prefix to ng generate library to choose your own.
The public API file
Only what public-api.ts exports is visible to consumers. Everything else is internal. This file is the contract of your library.
// projects/shared-ui/src/lib/badge.component.ts
import { Component, input } from '@angular/core';
@Component({
selector: 'lib-badge',
standalone: true,
template: `<span class="badge">{{ label() }}</span>`,
})
export class BadgeComponent {
label = input.required<string>();
}// projects/shared-ui/src/public-api.ts
export * from './lib/badge.component';If you forget to export a symbol here, the library still builds. The application that imports it will fail with a "has no exported member" error.
How an application finds the library
The generator adds a paths entry to the root tsconfig.json:
{
"compilerOptions": {
"paths": {
"shared-ui": ["./dist/shared-ui"]
}
}
}This maps the import name shared-ui to the library's build output in dist/, not to its source. An application imports it like any npm package:
// projects/storefront/src/app/app.component.ts
import { Component } from '@angular/core';
import { BadgeComponent } from 'shared-ui';
@Component({
selector: 'app-root',
standalone: true,
imports: [BadgeComponent],
template: `<lib-badge label="New" />`,
})
export class AppComponent {}Working on a library and an app together
Rebuilding by hand after every edit is slow. Run the library build in watch mode in one terminal and the app in another:
ng build shared-ui --watch --configuration development
ng serve storefrontThe first command rewrites dist/shared-ui on each change. The dev server notices the new output and reloads.
An alternative is to point paths at the library source, for example "shared-ui": ["./projects/shared-ui/src/public-api.ts"]. That removes the build step during development. The cost is that the app compiles the library as its own code. This hides packaging mistakes such as a missing export or a wrong peerDependencies entry, which then show up only after you publish. Many teams keep the dist/ mapping so local builds behave like the published package.
Publishing and dependencies
After a production build, publish from the output folder, not from the project folder:
ng build shared-ui
cd dist/shared-ui
npm publishThe generated library package.json lists @angular/core and @angular/common under peerDependencies. This tells consumers to supply their own Angular version, so the library does not bundle a second copy. If the library needs another package, list it in dependencies, and ng-packagr will warn about anything that is not declared. Use peerDependencies for anything that must be a single shared instance.
Common errors
- "Cannot find module 'shared-ui'" means the library has not been built, or
dist/was cleaned. Runng build shared-ui. - Changes in the library do not appear in the app. The app reads
dist/. Rebuild the library or use watch mode. - A symbol is "not exported". Add it to
public-api.ts. - "Cannot determine project for command". You ran a command from the workspace root without a project name. Pass one.
- A library imports from an application, or from another library's
projects/path. Thepathsmapping does not cover this, and the dependency direction is wrong. Libraries must not depend on applications. If library A needs library B, build B first and import it by its package name.
When to use a multi-project workspace
Use one when several applications share code and you want one set of dependency versions and one repository. Use one when you plan to publish a package and want an application beside it to test it.
Stay with a single project when you have one application. The extra structure adds build ordering and path configuration with no benefit. Very large organizations with many teams often move to a dedicated monorepo tool. A multi-project workspace is a simpler middle step.
Exercise
You cloned a multi-project workspace, ran ng serve storefront, and got "Cannot find module 'shared-ui'". The shared-ui library source exists in projects/shared-ui. What caused this, and what is the fix?
Show answer
The root tsconfig.json maps shared-ui to ./dist/shared-ui, which is the library's build output. A fresh clone has no dist/ folder. Run ng build shared-ui first, then ng serve storefront. For ongoing work, run ng build shared-ui --watch --configuration development in a second terminal.
Exercise
A teammate added a new TooltipDirective to projects/shared-ui/src/lib/ and rebuilt the library. The storefront app reports that shared-ui has no exported member TooltipDirective. What is missing?
Show answer
The directive is not exported from projects/shared-ui/src/public-api.ts. Add export * from './lib/tooltip.directive'; (using the real file name) and rebuild the library.