Chapter 2: Project Setup
Before we dive into components, directives, and state management, we need to set up an Angular project. Angular offers a powerful CLI (Command Line Interface) that simplifies installation, scaffolding, and development.
2.1 Prerequisites
To follow along, ensure you have:
- Node.js (LTS version recommended, e.g., v18+).
- npm (comes with Node) or yarn/pnpm as a package manager.
- A code editor (VS Code is highly recommended).
Check your installation:
node -v
npm -v
2.2 Installing Angular CLI
The Angular CLI makes it easy to create and manage projects.
Install globally:
npm install -g @angular/cli
Verify installation:
ng version
2.3 Creating a New Project
Generate a new Angular project using:
ng new my-app
CLI prompts:
- Would you like to add Angular routing? β Yes (so we have navigation support).
- Which stylesheet format would you like to use? β Choose (CSS, SCSS, or LESS).
Navigate to the project folder:
cd my-app
Start the development server:
ng serve
Open http://localhost:4200 to see your app running. π
2.4 Project Structure
The generated app contains several files and folders. Letβs break them down:
my-app/
βββ src/
β βββ app/
β β βββ app.component.ts # Root component logic
β β βββ app.component.html # Root component template
β β βββ app.component.css # Styles for root component
β β βββ app.routes.ts # Routes definition (standalone-first)
β βββ assets/ # Static assets (images, icons)
β βββ index.html # Entry point HTML
β βββ main.ts # Bootstraps Angular app
β βββ styles.css # Global styles
βββ angular.json # CLI configuration
βββ package.json # Dependencies and scripts
βββ tsconfig.json # TypeScript configuration
βββ README.md # Project instructions
Key files:
main.ts: Entry point that bootstraps the Angular app.app.component.ts: Root component.app.routes.ts: Defines routing (standalone-first).angular.json: CLI build and serve config.
2.5 Bootstrapping with Standalone Components
By default (Angular 17+), projects use standalone-first architecture.
main.ts:
import { bootstrapApplication } from '@angular/platform-browser';
import { provideRouter } from '@angular/router';
import { AppComponent } from './app/app.component';
import { routes } from './app/app.routes';
bootstrapApplication(AppComponent, {
providers: [provideRouter(routes)]
});
π No AppModule is required β the app starts with AppComponent.
2.6 Development Workflow
Common CLI commands:
ng serveβ Run dev server.ng generate component helloβ Generate a component.ng generate service authβ Generate a service.ng testβ Run unit tests.ng buildβ Build for production.
2.7 Summary
- Installed Angular CLI for scaffolding and management.
- Created a new Angular project with
ng new. - Explored the project structure and key files.
- Learned that Angular 17+ uses standalone-first bootstrapping.
- Got familiar with common development commands.
In the next chapter, weβll dive into Components, the building blocks of Angular applications, learning how to create, structure, and reuse them.