Sub topic 1
Creating the project with the Angular CLI
This chapter sets up the project that every later chapter builds on. You will install the Angular CLI, generate a new app, look at what it created, and run it locally.
Prerequisites
This chapter assumes you already know the following:
- JavaScript fundamentals: functions, arrays, objects, and modules.
- Basic TypeScript: types, interfaces, and classes.
- HTML and CSS basics.
- Node.js and npm installed. Run
node --versionto check. The Angular CLI requires a current LTS release of Node.js, and the CLI prints an error if your version is unsupported.
What the Angular CLI does
The Angular CLI is a command-line tool named ng. It creates projects, generates code, starts a development server, runs tests, and builds the app for production. Using it means you don't have to wire up TypeScript, the bundler, and the test runner yourself.
The five commands you will use in this tutorial are:
| Command | Purpose |
|---|---|
| `ng new` | Create a new project |
| `ng serve` | Start the development server |
| `ng generate` | Create components, services, and other files |
| `ng test` | Run unit tests |
| `ng build` | Produce a production build |
Creating the project
Install the CLI globally, create the project, move into it, and start the server:
npm install -g @angular/cli
ng new task-list
cd task-list
ng serveng new asks a few questions. Use these answers, or accept the defaults if a prompt differs in your CLI version:
- Stylesheet format: CSS keeps things simple.
- Server-side rendering (SSR): No. This app runs only in the browser.
The CLI then creates the folder, writes the files, installs dependencies, and initializes a git repository. This can take a minute.
After ng serve finishes building, open http://localhost:4200 in a browser. You should see the starter page. The server rebuilds and reloads the page each time you save a file.
Project structure
Most of your work happens in the src/app folder. These are the files that matter now:
| Path | Role |
|---|---|
| `src/main.ts` | Entry point that starts the app |
| `src/index.html` | The single HTML page that hosts the app |
| `src/styles.css` | Global styles |
| `src/app/app.ts` | The root component (in older CLI versions, `app.component.ts`) |
| `src/app/app.config.ts` | Application-wide providers, such as the router |
| `src/app/app.routes.ts` | The route definitions |
| `angular.json` | Build, serve, and test settings |
| `package.json` | Dependencies and npm scripts |
The names of the root component files vary between CLI versions. Everything else in this tutorial works the same either way.
The startup flow is short. main.ts boots the root component using the settings in app.config.ts. The root component's selector, app-root, appears as a tag in index.html, and Angular renders the component there.
Checking that the loop works
Make a small edit to confirm that live reload is working. Open the root component and change its template or title text, then save. The browser updates without a manual refresh.
If the page does not update, check the terminal where ng serve is running. Build errors, such as a TypeScript type error or a mistyped import, appear there and stop the page from updating. Fix the error and save again.
You can stop the server with Ctrl+C. If port 4200 is already in use, run ng serve --port 4300 to pick another one.
Other commands you will need later
You will meet these commands in later chapters. You don't need to run them yet, but it helps to know they exist.
ng test
ng buildng test runs unit tests in watch mode. ng build writes a production build to the dist folder.
Exercise
You ran ng serve, edited and saved a component, but the browser still shows the old page. What is the first place to look, and why?
Show answer
Look at the terminal running ng serve. If the edit introduced a build error, such as a type error or a bad import, the server reports it there and does not update the page until you fix it.
Exercise
Which file lists the application-wide providers, and which file starts the app?
Show answer
src/app/app.config.ts lists the application-wide providers, and src/main.ts starts the app by bootstrapping the root component with that configuration.