Sub topic 11
Running and building with ng serve and ng build
This chapter assumes you have a workspace created with ng new, and that you can read the architect section of angular.json from the previous chapter. It covers the two commands you will run most often: ng serve for development and ng build for producing deployable files.
What each command does
Both commands read the build target in angular.json. They differ in what they do with the result.
| Command | Purpose | Output |
|---|---|---|
| `ng serve` | Build and serve the app for development | Held in memory, not written to disk |
| `ng build` | Build the app once | Files written to `dist/<project-name>` |
Running the development server
ng serve compiles the application, starts a local web server, and rebuilds when you save a file. The default address is http://localhost:4200.
ng serve
ng serve --port 4300 --open--port changes the port and --open launches your browser. Under the hood, ng serve runs the serve target in angular.json. That target points at the build target, so options you set on build apply to the dev server too.
ng serve uses the development configuration by default in new workspaces. That configuration turns off optimization and enables source maps, so rebuilds are fast and you can debug your TypeScript in the browser.
Building for deployment
ng build compiles the app once and writes the result to the dist folder.
ng build
ng build --configuration developmentWithout a flag, the CLI uses the defaultConfiguration of the build target. In the angular.json excerpt from earlier, that is production. A production build minifies code, removes unused code (tree shaking), and adds a content hash to file names through outputHashing: "all". A hashed name such as main-3F2A9C1B.js changes whenever the content changes, so browsers can cache files for a long time and still fetch updates.
The output typically contains index.html, hashed JavaScript bundles, and your styles. In recent versions with the application builder, the deployable files are placed in dist/my-app/browser.
Common errors
- Port already in use. Another process holds port 4200. Stop it or run
ng serve --port 4300. - Deploying the wrong folder. Upload the contents of
dist/my-app/browser, not the whole project. - Production-only failures. Code that works under
ng servecan fail in a production build because of optimization or stricter budget checks. Runng buildbefore you deploy, not onlyng serve. - Blank page after deployment. If the app is hosted in a sub-path, set the base href:
ng build --base-href /my-app/.
Exercise
You run ng serve and then look for a dist folder, but it does not exist. Why, and which command creates it?
Show answer
ng serve keeps the compiled output in memory and serves it from there, so it writes nothing to disk. ng build writes the output to dist/<project-name>.
Exercise
Your angular.json sets defaultConfiguration to production on the build target. How do you run a build with source maps and no optimization?
Show answer
Run ng build --configuration development, which applies the development configuration (optimization: false, sourceMap: true) instead of the default.