← Back to Angular, page 8

angular-0146 · Hard senior

How would you organize a multi-app Angular workspace with shared libraries, and how do tsconfig paths, project references and build order work?

One angular.json workspace holds several applications and libraries. Libraries expose a public API file, and tsconfig paths map import names to source folders so apps import them like packages. Build libraries before apps, enforce import boundaries with lint rules, and use Nx or caching tools for affected builds in large repos.

Full answer

A workspace with several applications (admin, student portal) and shared code needs structure, or it becomes a tangle.

Layout: ng generate application admin and ng generate library ui add projects under projects/ (or apps/ and libs/ in Nx). Each project has its own tsconfigs and entry in angular.json.

Public API: each library has src/public-api.ts that re-exports what other projects may use. Anything not exported is private. Tools like ng-packagr build the library to a package. With secondary entry points (@school/ui/button) consumers can tree shake parts.

Resolution: in tsconfig.json, paths maps names to source: "@school/ui": ["projects/ui/src/public-api.ts"]. Apps import @school/ui in development and the application builder compiles library source together with the app (no pre-build step needed for path-mapped sources). If you publish the library, build it with ng build ui and consumers install the package. With dist mapping ("@school/ui": ["dist/ui"]) apps depend on the built output, so you must build libraries first.

Architecture rules:

  • Layers: apps depend on feature libraries, features depend on UI and data-access libraries, shared utilities depend on nothing. No cycles. Enforce with ESLint @nx/enforce-module-boundaries or eslint-plugin-boundaries and tags.
  • Keep libraries small and focused (feature, ui, data-access, util). Avoid a huge "shared" dump.
  • Do not import another library's internal file paths. Only the public API.
  • One version of Angular and key dependencies for the whole repo, to avoid duplicates.

Tooling for scale: Nx (or Turborepo and similar) gives a task graph, local and remote caching, and affected commands so CI builds only what changed. ng build alone rebuilds the app, but does not know the graph of your libraries for caching.

Pitfalls: circular dependencies between libraries, path mappings that differ between build and test tsconfigs (tests fail to resolve), and libraries importing app code. Keep a shared tsconfig.base.json that other configs extend.

typescript
// file: tsconfig.base.json
{
  "compilerOptions": {
    "strict": true,
    "paths": {
      "@school/ui": ["projects/ui/src/public-api.ts"],
      "@school/ui/button": ["projects/ui/button/src/public-api.ts"],
      "@school/data": ["projects/data/src/public-api.ts"]
    }
  }
}

// file: projects/ui/src/public-api.ts
export * from './lib/card.component';
export * from './lib/modal.service';
// Not exported here means private to the library.

// file: projects/ui/src/lib/card.component.ts
import { Component, input } from '@angular/core';

@Component({ selector: 'ui-card', template: `<h3>{{ title() }}</h3><ng-content />` })
export class CardComponent {
  readonly title = input('');
}

// file: projects/ui/src/lib/modal.service.ts
import { Injectable } from '@angular/core';

@Injectable({ providedIn: 'root' })
export class ModalService {
  open(name: string): string {
    return `opened ${name}`;
  }
}

The sample is configuration data (a tsconfig base and a library public API). It is type-checked, and the tsconfig file was parsed as JSON. Nothing was built with `ng-packagr` or Nx here.

Key points

  • Apps and libraries are projects in one workspace, each exposing a `public-api.ts`.
  • `tsconfig` `paths` map import names to source or built output.
  • Enforce layers and import boundaries with lint rules. Avoid cycles and a giant shared library.
  • Use Nx or similar for caching and affected builds in large repos.

Common mistakes

  • Importing deep internal files of another library.
  • Creating circular dependencies between libraries.
  • Different `paths` in build and test configs.

Follow-ups

  • How do secondary entry points help tree shaking?
  • When would you publish a library rather than path map it?
  • How do you stop a feature library from importing an app?

Tips

  • Describe layers and tags.
  • Mention public API and boundaries.
  • Say when you would add Nx.

Evaluation rubric

  • Explains workspace structure and path mapping.
  • Covers boundaries and build order.
  • Mentions tooling for scale.