Sub topic 6
Serving the App Locally with ng serve
This chapter assumes you have already created a workspace with ng new (here called my-app) and can run commands from its root folder. You should also know basic npm usage and be comfortable reading JSON configuration files. You will learn what ng serve does internally, how to configure it, which errors come up most often, and how it differs from the production build made by ng build.
What ng serve Actually Does
ng serve runs a local development server. It compiles your TypeScript, templates and styles, keeps the output in memory, serves it over HTTP, and watches your source files. When a file changes, it rebuilds only what is affected and updates the browser.
On current CLI versions (17 and later), new projects use the application builder. It compiles with esbuild and serves through a Vite-based dev server. Older projects may still use the webpack-based builder. The command and most options are the same for both, but rebuilds are faster with the esbuild/Vite pipeline.
The dev server does not write files to dist/. If you look for build output after running ng serve, you won't find any. Only ng build writes to disk.
Running the Server
From the workspace root:
cd my-app
ng serveWhen compilation finishes, the terminal prints a summary of the initial bundles and a local URL, normally http://localhost:4200/. Open it in a browser, or ask the CLI to open it for you:
ng serve --openStop the server with Ctrl+C. If you didn't install the CLI globally, run it through npm instead. The generated package.json includes a start script that calls ng serve:
npm start
# or, with extra flags passed through to ng serve
npm start -- --open --port 4300Common Options
| Option | Default | Purpose |
|---|---|---|
| `--open`, `-o` | `false` | Open the default browser after the first build |
| `--port` | `4200` | Port to listen on |
| `--host` | `localhost` | Network interface to bind to |
| `--configuration`, `-c` | `development` | Which build configuration from `angular.json` to use |
| `--proxy-config` | none | Path to a proxy file for forwarding API calls |
| `--ssl` | `false` | Serve over HTTPS with a generated certificate |
| `--hmr` | enabled on recent versions | Hot module replacement for styles and templates |
| `--watch` | `true` | Rebuild on file changes |
These flags can be combined:
ng serve --port 4300 --open --configuration developmentWhere the Defaults Come From: angular.json
Every option you pass on the command line overrides a value in the serve target of angular.json. Below is a trimmed version of what a recent CLI generates. The builder name is @angular/build:dev-server on CLI 20 and later, and @angular-devkit/build-angular:dev-server on earlier versions.
{
"projects": {
"my-app": {
"architect": {
"serve": {
"builder": "@angular/build:dev-server",
"configurations": {
"production": {
"buildTarget": "my-app:build:production"
},
"development": {
"buildTarget": "my-app:build:development"
}
},
"defaultConfiguration": "development"
}
}
}
}
}buildTarget points the dev server at a configuration of the build target. The development build configuration turns off optimization and turns on source maps, so you can debug the original TypeScript in browser DevTools. To set your own defaults (for example, always port 4300), add an options object to the serve target:
{
"serve": {
"builder": "@angular/build:dev-server",
"options": {
"port": 4300,
"open": true
},
"defaultConfiguration": "development"
}
}Check the result with ng serve --configuration production. It runs the optimized build through the dev server, which helps you find bugs that only show up with minification or stricter budgets. It is still a development server, though.
Proxying API Calls to a Backend
During development your Angular app runs on port 4200 and your API might run on port 3000. Calls from the browser to http://localhost:3000 are cross-origin, so they hit CORS restrictions. A proxy avoids this. The browser calls /api/... on port 4200, and the dev server forwards the request to the backend.
Create proxy.conf.json at the workspace root:
{
"/api": {
"target": "http://localhost:3000",
"secure": false,
"changeOrigin": true
}
}Then start the server with it:
ng serve --proxy-config proxy.conf.jsonYou can also make it permanent by adding "proxyConfig": "proxy.conf.json" to the serve target's options. In your code, use relative URLs such as /api/todos so that the same code works behind the proxy in development and behind a real reverse proxy in production.
Live Reload and Hot Module Replacement
When you save a file, the dev server rebuilds. On recent CLI versions, hot module replacement (HMR) is on by default for component styles and templates. Those edits are patched into the running page, and component state usually survives. Changes to TypeScript logic, such as a component class or a service, still trigger a full page reload, which resets the application state.
If HMR causes odd behavior, for example stale state after a template change, turn it off to rule it out:
ng serve --no-hmrTesting on Other Devices
By default the server binds to localhost, so only your own machine can reach it. To test on a phone on the same network, bind to all interfaces:
ng serve --host 0.0.0.0Then browse to http://<your-machine-ip>:4200. The CLI prints a warning, and the warning is accurate. Anyone on that network can now reach an unoptimized build with full source maps. Use this on trusted networks only. If requests are rejected because of an unrecognized hostname (common with tunnels or custom local domains), add the hostname to the allowedHosts option of the serve target rather than disabling host checking.
Common Errors and Fixes
| Symptom | Likely cause | Fix |
|---|---|---|
| "Port 4200 is already in use" | Another `ng serve` or process holds the port | Accept the CLI's offer of a different port, pass `--port`, or stop the other process |
| `ng: command not found` | CLI not installed globally | Use `npx ng serve` or `npm start` |
| Compilation errors in the terminal and an error overlay in the browser | Type errors or template errors | Fix the reported file and line. The server recovers on the next save |
| API calls return 404 under `/api` | Proxy not loaded or wrong path prefix | Confirm `--proxy-config` is used and the key matches the request path |
| Changes don't appear | Editing files outside `src`, or the watcher hit OS limits | Restart the server. On Linux, raise `fs.inotify.max_user_watches` |
| Node version error at startup | Node release not supported by this CLI version | Install an active or maintenance LTS Node release |
Angular type-checks templates during the dev build. A wrong property name in a template therefore fails the build, where in plain JavaScript it would only fail at runtime. Treat terminal errors as real failures, not warnings to scroll past.
From ng serve to a Production Build
ng serve is for development only. It is not hardened, tuned, or meant to face the public internet. For deployment, create a production build:
ng buildng build uses the production configuration by default. That means ahead-of-time compilation, tree-shaking, minification, hashed file names for cache busting, and no source maps. With the application builder, the output lands in dist/my-app/browser/. It contains index.html plus static JavaScript and CSS files that any static web server or CDN can host.
| Aspect | `ng serve` | `ng build` |
|---|---|---|
| Default configuration | `development` | `production` |
| Output location | Memory only | `dist/my-app/` on disk |
| Optimization | Off | Minified, tree-shaken |
| Source maps | On | Off |
| File watching | On | Off (unless `--watch`) |
| Proxy support | Yes | Not applicable |
| Intended use | Local development | Deployment |
During a build, the CLI enforces the budgets defined in the build target of angular.json. If the initial bundle grows past the warning threshold, you get a warning. Past the error threshold, the build fails. Read budget failures as a sign that you have added a heavy dependency. Raise the limit only when you have decided you need that dependency.
To preview the production output locally, serve the folder with any static server:
ng build
npx http-server dist/my-app/browser -p 8080Exercise
You run ng serve, then look in the dist/ folder and find it empty or missing. Is something broken? Explain why.
Show answer
Nothing is broken. ng serve compiles and serves the app from memory and never writes build output to disk. Only ng build writes files to dist/my-app/.
Exercise
Your frontend calls /api/users and your backend runs on http://localhost:5000. Write the proxy file and the command that starts the dev server with it.
Show answer
Create proxy.conf.json containing {"/api": {"target": "http://localhost:5000", "secure": false, "changeOrigin": true}}, then run ng serve --proxy-config proxy.conf.json. Alternatively, set "proxyConfig": "proxy.conf.json" in the serve target's options in angular.json.
Exercise
A bug appears only on the deployed site, not under ng serve. Which command helps you reproduce it locally while still using the dev server, and why might it help?
Show answer
Run ng serve --configuration production. It serves the production build configuration, with optimization and minification turned on, so bugs caused by production-only settings can show up locally.
Exercise
You start the server with ng serve --host 0.0.0.0 at a coffee shop to test on your phone. What risk does this create?
Show answer
Binding to 0.0.0.0 makes the unoptimized development build, including source maps that reveal your original source code, reachable by anyone on that public network. Use it only on trusted networks.