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 serveis 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:4200by 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 servewhile developing your application to quickly test changes and see how the app behaves in real-time.
- You use
Example:
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 buildis 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 buildcreates an optimized production build (with optimizations like minification, tree-shaking, etc.) in thedist/folder of the project.For a production build (with optimizations like minification and AOT compilation), you should use:
Use Case:
- You use
ng buildwhen you want to prepare the app for deployment to a live server or production environment.
- You use
Example:
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).
- The command generates static files (JavaScript, CSS, HTML, etc.) in the
Key Differences Between ng serve and ng build:
| Feature | ng serve | ng build |
|---|---|---|
| Purpose | Runs a development server, serves the app | Compiles and bundles the app for production |
| Environment | Development environment | Production or staging environment |
| Usage | Used during development to test the app | Used to generate a build for deployment |
| Optimizations | No optimizations by default | Optimized for performance (with --prod) |
| Watching Files | Watches for file changes and reloads the app | Does not watch files, just builds the app |
| Server | Starts a local development server (localhost:4200) | Does not start a server, just generates static files |
| Build Output | Runs a development build, served directly | Outputs build files to the dist/ folder |
Key Scenarios:
- During Development:
- You would use
ng serveto run the app locally, view it in the browser, and see live updates as you modify your code.
- You would use
- Before Deployment:
- You would use
ng build --prodto generate a production build that you can deploy to a server (e.g., Firebase, AWS, or any static web hosting service).
- You would use
Summary
ng serveis used for development purposes. It compiles your app and runs it on a local server with live-reload enabled.ng buildis 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
Post a Comment