Video lecture
Watch on YouTube(opens in a new tab)Sub topic 6
Serving the App Locally with ng serve
This chapter assumes you have created a workspace with ng new and can run commands from a terminal inside that folder. You will start the development server, see how it reacts to code changes, and compare it with a production build.
What ng serve does
ng serve compiles your application in memory and hosts it on a local web server. It does not write files to disk. Recent Angular versions use the application builder, which compiles with esbuild and serves through Vite. That setup makes startup and rebuilds fast.
The server watches your source files. When you save a change, the CLI rebuilds only what changed and the browser refreshes. Style changes are often applied without a full page reload.
cd my-app
ng serve --openThe terminal prints the local address, which is http://localhost:4200/ by default. The --open flag (short form -o) opens that address in your default browser. To stop the server, press Ctrl+C.
Useful options
| Option | Purpose | Example |
|---|---|---|
| `--open`, `-o` | Open the browser after the first build | `ng serve -o` |
| `--port` | Use a port other than 4200 | `ng serve --port 4300` |
| `--host` | Accept connections from other devices on your network | `ng serve --host 0.0.0.0` |
| `--configuration` | Pick a build configuration from `angular.json` | `ng serve --configuration production` |
By default, ng serve uses the development configuration. That configuration includes source maps and skips minification, so stack traces in the browser point to your TypeScript lines.
Building for production
When you are ready to deploy, create an optimized build:
ng buildng build uses the production configuration by default. It minifies code, removes unused code through tree shaking, and adds content hashes to file names so browsers cache them safely. The output goes to dist/my-app/browser/. You upload that folder to any static web host.
To check the build on your own machine, serve the folder over HTTP:
npx http-server dist/my-app/browser -p 8080Common errors
- "This command is not available when running the Angular CLI outside a workspace." You ran
ng servein a folder without anangular.jsonfile. Change into the project folder first. - "Port 4200 is already in use." Another server is still running. The CLI asks whether to use a different port. You can also stop the other process or pass
--port. - A blank page when you open
dist/.../index.htmldirectly. Browsers block module scripts loaded fromfile://URLs. Serve the folder over HTTP instead. - Changes do not appear. Make sure you saved the file and check the terminal for compile errors. The CLI keeps serving the last successful build until you fix the error.
Exercise
You start ng serve and get a message that port 4200 is in use. You also want a teammate on the same Wi-Fi network to open your running app. Which single command does both?
Show answer
ng serve --port 4300 --host 0.0.0.0 serves on a free port and accepts connections from other devices, so the teammate can browse to http://<your-ip>:4300/.
Exercise
Name two ways the output of ng build differs from what ng serve gives you by default.
Show answer
ng build writes files to dist/my-app/browser/ on disk, while ng serve keeps the build in memory. ng build also uses the production configuration, with minification, tree shaking and hashed file names. ng serve uses the development configuration, with source maps and no minification.