Difference between ng serve and ng build?

 In Angular, both ng serve and ng build are commands that you use during the development and deployment of your application. However, they serve different purposes and are used in different stages of your app's lifecycle. Here’s a detailed breakdown of the differences:

1. ng serve - Development Server Command

  • Purpose:

    • ng serve is used to serve your application in a local development environment. It starts a development server that watches for changes in the source code and automatically reloads the application (a.k.a. hot-reloading) whenever changes are made.
  • Common Usage:

    • It is primarily used during the development phase.
    • Runs the application on a local server so that you can view your app in the browser.
  • What It Does:

    • Compiles and bundles the application into a format that is immediately ready to be run in the browser.
    • It serves the application on a local server (usually on http://localhost:4200 by default).
    • Watches the files in the project for any changes and automatically rebuilds and reloads the application when changes are detected.
  • Use Case:

    • You use ng serve while developing your application to quickly test changes and see how the app behaves in real-time.
  • Example:

    bash

    ng serve
  • Outputs:

    • The command outputs the build process in the terminal and provides live updates as you make changes.
    • The app will be available at http://localhost:4200.

2. ng build - Production Build Command

  • Purpose:

    • ng build is used to build your Angular application for production or staging environments. It compiles your source code and prepares it for deployment.
  • Common Usage:

    • Used to generate a production-ready version of your app that can be deployed to a server.
  • What It Does:

    • Compiles and bundles all the application code into static files (HTML, CSS, JavaScript, etc.) that can be served by a web server.

    • By default, ng build creates an optimized production build (with optimizations like minification, tree-shaking, etc.) in the dist/ folder of the project.

    • For a production build (with optimizations like minification and AOT compilation), you should use:

      bash

      ng build --prod
  • Use Case:

    • You use ng build when you want to prepare the app for deployment to a live server or production environment.
  • Example:

    bash

    ng build --prod
  • Outputs:

    • The command generates static files (JavaScript, CSS, HTML, etc.) in the dist/ directory.
    • The app is now optimized for performance (e.g., minified, tree-shaken, ahead-of-time (AOT) compiled).

Key Differences Between ng serve and ng build:

Featureng serveng build
PurposeRuns a development server, serves the appCompiles and bundles the app for production
EnvironmentDevelopment environmentProduction or staging environment
UsageUsed during development to test the appUsed to generate a build for deployment
OptimizationsNo optimizations by defaultOptimized for performance (with --prod)
Watching FilesWatches for file changes and reloads the appDoes not watch files, just builds the app
ServerStarts a local development server (localhost:4200)Does not start a server, just generates static files
Build OutputRuns a development build, served directlyOutputs build files to the dist/ folder

Key Scenarios:

  1. During Development:
    • You would use ng serve to run the app locally, view it in the browser, and see live updates as you modify your code.
  2. Before Deployment:
    • You would use ng build --prod to generate a production build that you can deploy to a server (e.g., Firebase, AWS, or any static web hosting service).

Summary

  • ng serve is used for development purposes. It compiles your app and runs it on a local server with live-reload enabled.
  • ng build is used for production purposes. It compiles and bundles your app, optimizing it for deployment.

In short, ng serve is for development and ng build is for preparing your app for production.

Comments

Popular posts from this blog

Is there a compatibility list for Angular / Angular-CLI and Node.js?

Error when trying to inject a service into an angular component "EXCEPTION: Can't resolve all parameters for component", why?

How to resolve CORS issue in Angular 12?