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 the task-list project, read the files it creates, run the development server, and use the generator commands you will need later.
Prerequisites
This chapter assumes you have:
- JavaScript fundamentals: functions, arrays, objects, and ES modules.
- Basic TypeScript: types, interfaces, and classes.
- HTML and CSS basics.
- Node.js and npm installed, and some comfort with a terminal.
Each Angular release supports only certain Node.js versions. Check the version compatibility table in the official Angular documentation before you install. Then confirm what you have:
node --version
npm --versionIf Node.js is older than the range the table lists, the CLI will refuse to run or will print a warning. Upgrade Node.js before continuing.
What the Angular CLI does
The Angular CLI is a command-line tool, installed as the ng command. It does four jobs:
- It creates a project with a working build configuration.
- It runs a development server.
- It generates code such as components and services.
- It runs your tests and produces production builds.
You could assemble these pieces by hand, but the CLI keeps the TypeScript, bundler, and test settings consistent with the Angular version you use. That is why this tutorial uses it for everything.
Installing the CLI
Install the CLI globally so the ng command is available in any directory:
npm install -g @angular/cli
ng versionng version prints the CLI version and the versions of the Angular packages it finds. Run outside a project, it shows only the CLI, Node.js, and your operating system. Run inside a project, it also lists the project's Angular packages.
Generating the project
Create the project with ng new:
ng new task-listThe CLI asks a few questions. The exact questions depend on your CLI version, but you will typically see these:
| Prompt | Recommended answer for this tutorial |
|---|---|
| Which stylesheet format would you like to use? | CSS |
| Do you want to enable Server-Side Rendering (SSR) and Static Site Generation (SSG/Prerendering)? | No |
| Which AI tools do you want to configure? (newer versions only) | None |
Server-side rendering builds the page's HTML on a server before the browser receives it. It adds a server entry point and extra configuration. This tutorial builds a client-rendered app, so skip it.
You can skip the prompts by passing the answers as flags. This is useful in scripts and in documentation, because the result is the same on every machine:
ng new task-list --style=css --ssr=falseIf you answer the prompts or pass flags, the CLI then does three things:
- Writes the project files into a new
task-listdirectory. - Runs
npm installto download the dependencies. - Initializes a git repository and makes a first commit.
Add --skip-git if you do not want the repository, or --skip-install if you want to run npm install yourself later.
Reading the project structure
Move into the project and look at what was generated:
cd task-listThe important entries are below. Names can differ slightly between CLI versions.
| Path | Purpose |
|---|---|
| `package.json` | Dependencies and the npm scripts `start`, `build`, and `test`. |
| `angular.json` | Workspace configuration: build targets, the dev server, test settings, and file replacements per environment. |
| `tsconfig.json` | TypeScript compiler options shared by the whole project. |
| `public/` | Static files such as the favicon, copied into the build output unchanged. |
| `src/index.html` | The single HTML page. It contains the root element where Angular mounts the app. |
| `src/main.ts` | The entry point that starts the application. |
| `src/styles.css` | Global styles. |
| `src/app/app.config.ts` | Application-wide providers, such as the router. |
| `src/app/app.routes.ts` | The route table. It starts empty. |
| `src/app/app.ts` | The root component, with its template and styles in `app.html` and `app.css`. |
Where the app starts
Open src/main.ts. The generated file is short:
import { bootstrapApplication } from '@angular/platform-browser';
import { appConfig } from './app/app.config';
import { App } from './app/app';
bootstrapApplication(App, appConfig).catch(err => console.error(err));bootstrapApplication takes the root component and the application configuration, then renders the component into the page. There is no NgModule here. Since Angular 17, new projects use standalone components, which declare their own dependencies. The next chapters rely on that.
Now open src/app/app.config.ts:
import { ApplicationConfig, provideBrowserGlobalErrorListeners } from '@angular/core';
import { provideRouter } from '@angular/router';
import { routes } from './app.routes';
export const appConfig: ApplicationConfig = {
providers: [provideBrowserGlobalErrorListeners(), provideRouter(routes)],
};The providers array is where you register application-wide features. Later chapters add provideHttpClient() here and extend the router setup. Older CLI versions generate a slightly different list, for example provideZoneChangeDetection. The shape is the same.
Running the development server
Start the server from the project directory:
ng serveThe CLI builds the app and serves it at http://localhost:4200/. It then watches your source files. When you save a change, it rebuilds and the browser reloads, so you see the result without restarting anything.
Useful options:
ng serve --openopens the browser for you.ng serve --port 4300uses a different port, which helps when 4200 is already taken.
Stop the server with Ctrl+C. If the terminal reports that the port is in use, another ng serve is probably still running in a different terminal.
Open src/app/app.html and replace its contents with a heading:
<h1>Task List</h1>
<router-outlet />Save the file. The browser updates and shows your heading. The <router-outlet /> element is where routed components will appear in a later chapter. It is harmless until then.
If your template stops compiling, the terminal shows the error with a file and line number, and the browser shows an overlay with the same message. Read the terminal first, because it keeps the full text.
Generating code with ng generate
The CLI can create the files for a component, service, or other building block, so you do not write the boilerplate by hand. The short form is ng g.
ng generate component task-list
ng generate service task
ng generate interface taskEach command creates files in src/app and prints the list. A component also gets a test file. With a recent CLI, the first command produces task-list.ts, task-list.html, task-list.css, and task-list.spec.ts, containing a standalone component class named TaskList.
Two options are worth knowing:
--dry-run(or-d) prints the files the command would create or change without writing anything. Use it whenever you are unsure what a generator will do.--skip-testsleaves out the.spec.tsfile. Avoid it here, because a later chapter tests the service.
To keep the .component and .service suffixes that this tutorial uses, set the type option on the command. This works in CLI versions that support it:
ng generate component task-list --type=component
ng generate service task --type=serviceWith --type=component, the files become task-list.component.ts, and so on. Generated class names also follow the type, giving TaskListComponent. If your CLI rejects the option, it predates this naming change and already generates the suffixed names.
Running tests and building
Two more commands complete the toolbox. Later chapters cover them in depth, but you should run them once now to check that your setup works:
ng test
ng buildng test runs the unit tests in watch mode. Depending on the CLI version, the test runner is Karma with Jasmine or Vitest. Karma opens a Chrome window, so it needs a browser installed. The default test generated for the root component should pass.
ng build compiles the app for production and writes the output to dist/task-list. The CLI prints the size of each bundle. A fresh project builds in seconds. If the build reports a budget error later, the limits live in angular.json.
Common errors
ng: command not found: the global install did not put npm's global bin directory on yourPATH. Runnpm config get prefixand add that directory'sbinfolder toPATH, or usenpx @angular/cli.- Node.js version warning or error: your Node.js is outside the supported range for this Angular version. Upgrade Node.js, or install a CLI version that matches your Node.js.
- Permission errors on global install (EACCES): do not use
sudo. Fix npm's global directory permissions or use a Node version manager. - Port 4200 already in use: stop the other server, or run
ng serve --port 4300. - Generated names differ from this tutorial: see the file-naming note above.
Exercise
You run ng generate component task-list and want to know which files it will write before it touches your project. Which option do you add, and what does it do?
Show answer
Add --dry-run (short form -d). The CLI prints the files it would create or modify, without writing anything to disk, so you can check paths and names first.
Exercise
Which file would you edit to register an application-wide provider such as provideHttpClient(), and which function in main.ts uses that file?
Show answer
Edit src/app/app.config.ts and add the provider to the providers array of appConfig. bootstrapApplication in src/main.ts receives appConfig as its second argument and registers those providers when the app starts.