Skip to content
tezvyn:

ng serve purpose and fundamental difference from ng build

Source: angular.devEasyHow cards are made

ng serve purpose and fundamental difference from ng build
Summary

Understanding in-memory dev serving versus disk builds.

Key points

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

Watch out for

Believing ng serve writes files to disk.

What's really being asked

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.

The full answer

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.

The mistakes people make

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.

What usually comes next

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.

A 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.

Interview question

You need to generate deployable files to upload to a web server. Why should you use ng build instead of ng serve?

  • a.ng serve produces production-optimized bundles by default, while ng build uses development settings
  • b.ng serve writes files to disk just like ng build, but only includes development source maps
  • c.ng build writes the compiled output to the dist/ folder, while ng serve keeps the build in memory and starts a dev serverCorrect
  • d.ng build automatically starts a local server on port 4200, while ng serve requires a separate web server
Why?

ng build writes static files to the dist/ folder for deployment, while ng serve keeps output in memory and runs a dev server. Many beginners incorrectly think ng serve also writes files to disk, but it never produces disk output.

Just read this? Test yourself on what you have been reading.

Read the original → angular.dev

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on angular — each one lists the topics its interview covers.

See open roles