Let’s understand the difference between ng serve and ng build in Angular. Two essential commands you’ll encounter when working with the Angular CLI. They serve distinct purposes in the development and deployment lifecycle, and understanding their roles will help you navigate your Angular projects more effectively. We will break it down step-by-step, with examples and key distinctions.
What is ng serve?
ng serve is a development command that builds and serves your Angular application locally. It’s designed for real-time development, providing a fast feedback loop with features like automatic rebuilding and live reloading.
How It Works
- Builds the App: It compiles your TypeScript, HTML, and CSS into JavaScript and other assets.
- Serves Locally: It spins up a development web server (usually at http://localhost:4200).
- Watches for Changes: It monitors your source files and automatically recompiles and reloads the browser when you save changes.
- Development Mode: The output is optimized for debugging (e.g., source maps are included), not production performance.
Command
bash
ng serve
Example Usage
Imagine you’re working on a simple Angular app. You run:
bash
ng serve
- The terminal shows: Angular is running on http://localhost:4200/.
- You open the browser, see your app, and start editing app.component.html. Each save instantly reflects in the browser without restarting the server.
Key Features
- Live Reload: Browser updates without manual refresh.
- Fast Incremental Builds: Only changed files are recompiled.
- Default Port: 4200 (customizable with –port).
Options
- ng serve –open: Opens the browser automatically.
- ng serve –port 5000: Runs on a custom port.
- ng serve –configuration=dev: Uses a specific environment config.
Output
The output isn’t written to disk as a standalone artifact—it’s held in memory and served dynamically.
What is ng build?
ng build is a command that compiles your Angular application into a set of static files, typically for deployment. It’s a one-time build process, producing an output directory (usually dist/) that you can host on a web server.
How It Works
- Builds the App: Compiles your code into JavaScript, CSS, and HTML.
- Optimizes: Applies optimizations like minification and tree-shaking (removing unused code), depending on the configuration.
- Writes to Disk: Outputs files to a directory (default: dist/<project-name>).
- No Server: It doesn’t serve the app—you need a separate server (e.g., http-server) to view it.
Command
bash
ng build
Example Usage
You’re ready to deploy your app. You run:
bash
ng build
- The terminal shows: Generating build… and completes with files in dist/my-app/.
- You upload the dist/my-app/ folder to a hosting service (e.g., Firebase, Netlify) to make it live.
Key Features
- Production Ready: Use –prod (or –configuration=production) for optimized builds.
- Static Output: Files are ready for any static file server.
- No Watching: It’s a one-and-done process.
Options
- ng build –prod: Builds for production (minified, optimized).
- ng build –watch: Rebuilds on file changes (but doesn’t serve).
- ng build –output-path=dist/custom: Specifies a custom output directory.
Output
A dist/ folder with files like:
dist/my-app/ ├── index.html ├── main.js ├── styles.css └── assets/
Key Differences
Aspect | ng serve | ng build |
---|---|---|
Purpose | Development and testing | Building for deployment |
Output | In-memory, served dynamically | Static files in dist/ folder |
Server | Runs a local dev server | No server included |
Live Reload | Yes, auto-reloads on changes | No (unless using –watch) |
Optimization | Development mode (unminified, source maps) | Can be production-optimized (–prod) |
Use Case | Real-time coding and debugging | Preparing app for hosting/production |
Execution | Continuous (watches files) | One-time process |
Default Port | 4200 | N/A |
Practical Scenarios
When to Use ng serve
- You’re actively coding a feature (e.g., tweaking a component’s template or logic).
- You want instant feedback as you edit files.
- Example:bash
ng serve --open
You’re prototyping a form and testing it live in the browser.
When to Use ng build
- You’re done coding and ready to deploy.
- You need static files for a production server or testing.
- Example:bash
ng build --prod
You’ve finished your app and want to host it on GitHub Pages.
Combining Both
During development, you might:
- Use ng serve to build and test locally.
- Periodically run ng build to check the production output (e.g., file sizes, errors).
Example Workflow
import { Component } from '@angular/core'; @Component({ selector: 'app-root', template: '<h1>Hello, {{ name }}!</h1>', }) export class AppComponent { name = 'Angular'; }
- Development with ng serve:bash
ng serve
- Visit http://localhost:4200.
- Change name to ‘World’, save, and see “Hello, World!” instantly.
- Production with ng build:bash
ng build --prod
- Check dist/my-app/index.html—it’s minified and ready to deploy.
- Serve it with npx http-server dist/my-app to test locally.
Notes:
- ng serve Isn’t Production-Ready: Don’t use it for hosting—it’s not optimized and includes debugging overhead.
- ng build Needs a Server: After building, you’ll need a way to serve the files (e.g., ng serve won’t work on dist/).
- Configuration: Both commands respect angular.json configs (e.g., development vs. production environments).
Conclusion:
In short, ng serve is your go-to for development—quick, interactive, and hands-on—while ng build is for shipping your app to the world, producing a polished, deployable artifact. If you’re wondering how these fit into a specific workflow or need help troubleshooting either, just let me know!