tezvyn:

ng serve purpose and fundamental difference from ng build

AI-drafted, machine-checkedSource: angular.devbeginner
ng serve purpose and fundamental difference from ng build
WHAT IT TESTS

Understanding in-memory dev serving versus disk builds.

ANSWER OUTLINE

ng serve compiles in memory and serves via a dev server with live reload, while ng build writes output to dist/.

RED FLAG

Believing ng serve writes files to disk.

WHAT THIS TESTS: This question checks whether you understand the fundamental difference between a development-time server process and a static artifact generation step. The interviewer wants to know if you recognize that ng serve is optimized for speed and feedback during development, while ng build is designed to produce deployable files. It also reveals whether you know where the compiled output lives, how the CLI handles file watching, and why you should never use ng serve in a production environment.

A GOOD ANSWER COVERS: First, state that ng serve builds and serves your application, rebuilding on file changes. Second, emphasize that ng serve keeps the compiled output in memory rather than writing it to disk, which makes rebuilds faster. Third, explain that ng build compiles the application into an output directory named dist/ at the given output path. Fourth, note that ng build does not start a server or watch files by default, though it can be configured for file watching if needed. Fifth, mention that ng serve includes live reload via a development server, which ng build does not provide. Sixth, clarify that ng build is what you run before deploying to a web server or a content delivery network.

COMMON WRONG ANSWERS: One red flag is claiming that ng serve writes files to the dist/ directory just like ng build. Another is saying that ng build automatically starts a local server for you. Some candidates confuse the two commands because both perform compilation, but they miss the critical distinction of memory versus disk and server versus static output. A third mistake is asserting that ng serve produces production-optimized bundles by default, when in fact it uses development settings unless explicitly configured otherwise. A fourth error is suggesting that ng serve is suitable for hosting a production application.

LIKELY FOLLOW-UPS: The interviewer may ask how you would configure ng build for production, such as using the production configuration flag. They might ask where the dist/ folder is located and how to change the output path in angular.json. Another follow-up could be about the file-watching behavior and how to disable live reload during ng serve. You might also be asked about the differences in bundle optimization between development and production builds. Finally, they could ask how to serve the dist/ folder locally for smoke testing, which is when you might use a separate tool like npx serve or a configured nginx container.

ONE CONCRETE EXAMPLE: Imagine you are working on a new feature. You run ng serve to start the development server on localhost port 4200. As you edit a component TypeScript file, the CLI detects the change, recompiles the affected module in memory, and refreshes the browser automatically. When you finish the feature and want to deploy, you run ng build. The CLI writes the compiled JavaScript, HTML, and CSS into the dist/my-app/ directory. You then copy those static files to your web server or CDN. You do not use ng serve for deployment because it is not intended to serve production traffic and does not write files to disk.

Source: angular.dev

Read the original → angular.dev

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.