Chapter 02

Setup and your first application

Ten minutes to a running application, then a careful walk through every generated file — because the ones people ignore are exactly the ones that cause trouble later.

⏱ 14 min readLevel: beginnerHands-on

Installing

terminal
node --version        # 20 LTS or 22 — Angular 18 requires ≥ 18.19
npm install -g @angular/cli@18
ng version
Prefer not to install globally

npx @angular/cli@18 new orders-web works without a global install and pins the version per project — which matters when you maintain several applications on different Angular versions.

Generating the project

terminal
ng new orders-web \
  --style=scss \
  --routing \
  --ssr=false \
  --package-manager=npm

cd orders-web
ng serve --open
FlagChooseWhy
--stylescssNesting and variables; you will want them by week two
--routingyesAdds app.routes.ts — every real app needs it
--ssrno for nowAdds a Node server; enable later if you need SEO (chapter 12)
--standalonedefault since v17No NgModule scaffolding

What was generated

project tree
orders-web
├── angular.json          build/serve/test configuration — budgets live here
├── package.json
├── tsconfig.json         strict mode, path aliases
├── public/               copied verbatim into the build output
└── src
    ├── index.html        the only HTML page the server ever returns
    ├── main.ts           bootstraps the application
    ├── styles.scss       global styles
    └── app
        ├── app.config.ts     providers: router, http, interceptors
        ├── app.routes.ts     route table
        ├── app.component.ts  root component
        └── app.component.html
src/main.ts
import { bootstrapApplication } from '@angular/platform-browser';
import { appConfig } from './app/app.config';
import { AppComponent } from './app/app.component';

bootstrapApplication(AppComponent, appConfig)
  .catch(err => console.error(err));
src/app/app.config.ts — the application's composition root
import { ApplicationConfig, provideZoneChangeDetection } from '@angular/core';
import { provideRouter, withComponentInputBinding } from '@angular/router';
import { provideHttpClient, withInterceptors, withFetch } from '@angular/common/http';

import { routes } from './app.routes';
import { authInterceptor } from './core/auth/auth.interceptor';
import { errorInterceptor } from './core/http/error.interceptor';

export const appConfig: ApplicationConfig = {
  providers: [
    // coalescing batches change detection across a burst of events
    provideZoneChangeDetection({ eventCoalescing: true }),

    provideRouter(
      routes,
      withComponentInputBinding(),      // route params bind straight to input()s
    ),

    provideHttpClient(
      withFetch(),                      // uses fetch(); required for SSR streaming
      withInterceptors([authInterceptor, errorInterceptor]),
    ),
  ],
};

This file is the single place that answers "what is switched on in this application?". Every chapter from here adds one line to it.

Your first component

terminal
ng generate component features/orders/order-list --inline-style
# short form: ng g c features/orders/order-list
src/app/features/orders/order-list/order-list.component.ts
import { Component, signal } from '@angular/core';
import { CurrencyPipe, DatePipe } from '@angular/common';

interface Order {
  id: number;
  reference: string;
  total: number;
  status: 'PLACED' | 'PAID' | 'SHIPPED';
  placedAt: string;
}

@Component({
  selector: 'app-order-list',
  standalone: true,
  imports: [CurrencyPipe, DatePipe],
  template: `
    <h2>Orders</h2>

    <table class="table">
      <thead>
        <tr><th>Reference</th><th>Placed</th><th>Total</th><th>Status</th></tr>
      </thead>
      <tbody>
        @for (order of orders(); track order.id) {
          <tr>
            <td>{{ order.reference }}</td>
            <td>{{ order.placedAt | date:'short' }}</td>
            <td>{{ order.total | currency:'EUR' }}</td>
            <td><span class="badge">{{ order.status }}</span></td>
          </tr>
        } @empty {
          <tr><td colspan="4">No orders yet.</td></tr>
        }
      </tbody>
    </table>
  `,
  styles: `
    .table { width: 100%; border-collapse: collapse; }
    th, td { text-align: left; padding: .5rem .75rem; border-bottom: 1px solid #e5e7eb; }
  `,
})
export class OrderListComponent {
  readonly orders = signal<Order[]>([
    { id: 1, reference: 'ORD-1042', total: 128.00, status: 'PAID',
      placedAt: '2026-08-14T10:12:00Z' },
    { id: 2, reference: 'ORD-1043', total: 42.50,  status: 'PLACED',
      placedAt: '2026-08-15T09:03:00Z' },
  ]);
}
src/app/app.routes.ts
import { Routes } from '@angular/router';

export const routes: Routes = [
  { path: '', redirectTo: 'orders', pathMatch: 'full' },
  {
    path: 'orders',
    // lazily loaded: this component ships in its own JS chunk
    loadComponent: () =>
      import('./features/orders/order-list/order-list.component')
        .then(m => m.OrderListComponent),
  },
  { path: '**', loadComponent: () => import('./shared/not-found.component')
        .then(m => m.NotFoundComponent) },
];

Talking to the backend in development

The dev server runs on :4200, the API on :8080. Calling it directly triggers CORS. Proxy instead — then the browser only ever sees one origin, exactly as in production.

proxy.conf.json
{
  "/api": {
    "target": "http://localhost:8080",
    "secure": false,
    "changeOrigin": true,
    "logLevel": "debug"
  }
}
angular.json — serve options
"serve": {
  "options": {
    "proxyConfig": "proxy.conf.json"
  }
}

Now this.http.get('/api/orders') works in development and in production with no environment switch and no CORS configuration at all.

Bundle budgets — set them on day one

angular.json
"budgets": [
  { "type": "initial",         "maximumWarning": "500kB", "maximumError": "1MB" },
  { "type": "anyComponentStyle","maximumWarning": "4kB",  "maximumError": "8kB" }
]
A budget is the only thing that stops bundle creep

Nobody notices 40 KB added in a pull request. Everybody notices 2 MB six months later, when it is twenty commits' worth of work to unwind. Make the build fail instead.

TypeScript strictness

tsconfig.json
{
  "compilerOptions": {
    "strict": true,
    "noImplicitOverride": true,
    "noPropertyAccessFromIndexSignature": true,
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": true
  },
  "angularCompilerOptions": {
    "strictTemplates": true,
    "strictInjectionParameters": true,
    "typeCheckHostBindings": true
  }
}

strictTemplates is the important one: it type-checks your HTML. Passing a string where a component expects a number becomes a build error instead of a runtime surprise. Never turn it off.

The CLI commands you will actually use

CommandDoes
ng serveDev server with hot reload
ng buildProduction build into dist/
ng testUnit tests
ng generate c|s|d|p|gComponent, service, directive, pipe, guard
ng updateUpgrades Angular and runs code migrations
ng build --stats-jsonProduces data for bundle analysis
ng lintESLint, after ng add @angular-eslint/schematics
ng update is the reason Angular upgrades are boring

ng update @angular/core@19 @angular/cli@19 rewrites your source for renamed APIs automatically. Upgrade one major version at a time, commit between each, and the whole thing is usually an afternoon.

Troubleshooting the first run

SymptomCauseFix
'app-order-list' is not a known elementComponent not in the parent's importsAdd it to the standalone component's imports array
NG0201: No provider for HttpClientprovideHttpClient() missingAdd it to app.config.ts
Blank page, no errorsRoute did not matchCheck app.routes.ts and that <router-outlet> exists
CORS error in devCalling :8080 directlyUse the proxy config above
NG0100: ExpressionChangedAfterItHasBeenCheckedValue mutated during change detectionMove the write into a signal or an event handler

Why proxy /api in development rather than calling http://localhost:8080 directly?

With a proxy the relative path /api/orders works unchanged in development and in production, where nginx or the ingress performs the same routing. No CORS configuration and no environment switching.