Full answer
Two commands you use every day:
ng serve (also ng s) is for development. It builds the app, keeps the result in memory, starts a local server (http://localhost:4200), and rebuilds when you save a file. The browser updates through live reload, and in many cases through hot module replacement, which swaps styles and templates without losing app state. It uses the development configuration: no minification, source maps on, so you can debug the original TypeScript.
ng build creates files on disk, by default in dist/<project>/browser. Since Angular 17 the default configuration is production. It is the thing you deploy.
What a production build does:
- Minification: shorter variable names, no spaces, so files are small.
- Tree shaking: removes code that nothing uses.
- Ahead-of-time (AOT) compilation of templates, which is also used in development now.
- Output hashing: file names such as
main-AB12CD34.jsallow long caching. - Budgets: warnings or errors when the bundle gets bigger than the limits in
angular.json. - Optimised fonts and styles inlining (critical CSS), and no source maps by default.
Differences you can see: a development bundle is several times larger and has readable code. Errors in production show minified names unless you upload source maps to your error tracker.
Useful options: ng serve --open, --port 4300, --configuration production (to test a production-like build locally), ng build --configuration development (readable output), ng build --stats-json for bundle analysis, ng build --base-href /school/ for a sub path.
Never deploy the output of ng serve: it does not exist on disk, and it is not optimised. And remember that the dev server is only for your machine: it has no caching rules, no compression settings, and no server rendering behaviour that matches your production host.
// file: angular.json (excerpt)
{
"projects": {
"school-app": {
"architect": {
"build": {
"builder": "@angular/build:application",
"configurations": {
"production": {
"budgets": [
{ "type": "initial", "maximumWarning": "500kB", "maximumError": "1MB" }
],
"outputHashing": "all"
},
"development": {
"optimization": false,
"extractLicenses": false,
"sourceMap": true
}
},
"defaultConfiguration": "production"
},
"serve": {
"builder": "@angular/build:dev-server",
"configurations": {
"production": { "buildTarget": "school-app:build:production" },
"development": { "buildTarget": "school-app:build:development" }
},
"defaultConfiguration": "development"
}
}
}
}
}Angular CLI 20.3. The `ng build` output sizes were not measured for this answer, so the answer states differences in general terms. The excerpt shows the options from the CLI generated configuration.
Key points
- `ng serve` is a dev server with live reload, source maps and no optimisation.
- `ng build` writes files to `dist`, and production is the default configuration.
- Production does minification, tree shaking, hashing and budgets.
- Test production behaviour with `--configuration production` before release.
Common mistakes
- Deploying a development build and wondering why the app is slow.
- Debugging performance in the dev server and trusting the numbers.
- Forgetting that production errors need source maps to read.
Follow-ups
- What are budgets, and where are they configured?
- What does output hashing give you?
- How do you serve the production build locally?
Tips
- Contrast the two commands first, then the two configurations.
- List at least four production optimisations.
- Say that you test the production build before release.
Evaluation rubric
- Separates the commands and the configurations.
- Lists the optimisations correctly.
- Knows options for development and deployment checks.