How to identity the Difference between ng serve and ng build ?

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

Aspectng serveng build
PurposeDevelopment and testingBuilding for deployment
OutputIn-memory, served dynamicallyStatic files in dist/ folder
ServerRuns a local dev serverNo server included
Live ReloadYes, auto-reloads on changesNo (unless using –watch)
OptimizationDevelopment mode (unminified, source maps)Can be production-optimized (–prod)
Use CaseReal-time coding and debuggingPreparing app for hosting/production
ExecutionContinuous (watches files)One-time process
Default Port4200N/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:bashng serve --openYou’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:bashng build --prodYou’ve finished your app and want to host it on GitHub Pages.

Combining Both

During development, you might:

  1. Use ng serve to build and test locally.
  2. 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';
}
  1. Development with ng serve:bashng serve
    • Visit http://localhost:4200.
    • Change name to ‘World’, save, and see “Hello, World!” instantly.
  2. Production with ng build:bashng 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!

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *