Skip to content
Tutorials Center
My learning

Topics

Lesson 3 of 74 ยท Getting Started

Setup and the Angular CLI

Install Node and the Angular CLI, create a project with ng new, run it with ng serve and learn the project layout, angular.json, main.ts and app.config.ts.

You do not build an Angular application by hand-writing a webpage and adding a script tag. Angular code has to be compiled (TypeScript to JavaScript, templates to instructions), bundled and optimized, and doing that by hand would be painful. The Angular CLI does it for you. It creates the project, starts a development server, builds the production bundles, generates new files and runs your tests, all with one command called ng.

This lesson walks through installing the tools and creating a first project, then explains each file you will find in it. As promised in the introduction, the terminal sessions and project files here are real Angular, read-only: run them on your own computer. The two runnable examples are stand-ins that model what the CLI does.

What you need

  • Node.js. The CLI and the build tools run on Node. Install a current even-numbered LTS release from nodejs.org. Each Angular version supports a specific range of Node versions, listed in the official version-compatibility table, so check it if the CLI complains.
  • A package manager. npm comes with Node. pnpm and yarn also work.
  • A code editor. Visual Studio Code with the official Angular Language Service extension gives you template autocomplete and errors. Other editors have similar plugins.

Check Node and install the CLI globally:

Terminal
node --version
npm install -g @angular/cli
ng version

You can also skip the global install and run npx @angular/cli new my-app, which downloads the CLI just for that command.

Creating a project

Terminal
ng new my-app
cd my-app
ng serve

ng new asks a few questions: which stylesheet format you want (plain CSS, Sass and so on), whether to turn on server-side rendering and prerendering (see that lesson later), and sometimes whether to generate files for AI coding tools. For learning, plain CSS and no server rendering is a fine choice. When it finishes you have a folder with source files, a package.json and an installed node_modules.

ng serve compiles the app and starts a development server, by default at http://localhost:4200. Open that address and you see a starter page. Edit a file and save, and the browser reloads on its own. Stop the server with Ctrl+C.

The commands you will use daily

Command What it does
ng serve Build in development mode and serve with live reload
ng build Produce an optimized production build in the dist folder
ng generate component user-card Create a component (alias ng g c user-card)
ng generate service data Create a service (also pipes, directives, guards and more)
ng test Run unit tests
ng update Upgrade Angular packages and apply automatic code migrations
ng add Install a package and run its setup (for example Angular Material)

Run ng generate --help to see every generator, and ng <command> --help for the options of any command. Passing --dry-run to a generator prints what it would create without writing anything, which is a good way to explore.

The project layout

A new application (Angular 20 style) looks like this:

TEXT
my-app/
โ”œโ”€โ”€ angular.json          # project and build configuration
โ”œโ”€โ”€ package.json          # dependencies and npm scripts
โ”œโ”€โ”€ tsconfig.json         # TypeScript settings (strict by default)
โ”œโ”€โ”€ tsconfig.app.json     # settings for the application build
โ”œโ”€โ”€ tsconfig.spec.json    # settings for tests
โ”œโ”€โ”€ public/               # static files copied as they are (favicon, images)
โ””โ”€โ”€ src/
    โ”œโ”€โ”€ index.html        # the single HTML page
    โ”œโ”€โ”€ main.ts           # the entry point
    โ”œโ”€โ”€ styles.css        # global styles
    โ””โ”€โ”€ app/
        โ”œโ”€โ”€ app.ts            # the root component class
        โ”œโ”€โ”€ app.html          # its template
        โ”œโ”€โ”€ app.css           # its styles
        โ”œโ”€โ”€ app.spec.ts       # its test
        โ”œโ”€โ”€ app.config.ts     # application-wide providers
        โ””โ”€โ”€ app.routes.ts     # the route table

File names and the exact starter files change a little between versions. Older projects name the root files app.component.ts and call the class AppComponent; since version 20 the style guide drops those suffixes, and new projects use app.ts and a class named App. The ideas are identical.

index.html

It is a normal HTML file with one custom element in the body:

HTML
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <title>MyApp</title>
    <base href="/" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
  </head>
  <body>
    <app-root></app-root>
  </body>
</html>

The build adds the script tags for you. <app-root> is where Angular puts your root component.

main.ts and bootstrapApplication

main.ts is the first file that runs. It starts the application:

TypeScript
import { bootstrapApplication } from '@angular/platform-browser';
import { appConfig } from './app/app.config';
import { App } from './app/app';

bootstrapApplication(App, appConfig).catch((error) => console.error(error));

bootstrapApplication takes the root component and a configuration object. Angular creates the root component, finds the <app-root> element that matches its selector, and renders the component there.

flowchart LR
  A["index.html<br/>with app-root"] --> B["main.ts"]
  B --> C["bootstrapApplication(App, appConfig)"]
  C --> D["App component renders<br/>inside app-root"]
  D --> E["Child components<br/>and routed pages"]

The browser loads one HTML page; main.ts hands the root component to Angular, which fills in the rest.

app.config.ts

This file lists the providers for the whole application, the building blocks that Angular's dependency injection system makes available (later lesson):

TypeScript
import { ApplicationConfig } from '@angular/core';
import { provideRouter } from '@angular/router';
import { routes } from './app.routes';

export const appConfig: ApplicationConfig = {
  providers: [
    provideRouter(routes),
    // provideHttpClient(), provideZonelessChangeDetection() and others go here
  ],
};

A generated file may contain extra providers depending on your version and answers (for example error listeners or change-detection settings). Adding a feature to the app usually means adding one provideXyz() call here.

angular.json

angular.json tells the CLI how to build, serve and test each project in the workspace: the entry file, the global styles, the output folder and separate configurations for development and production. You rarely edit it by hand, but you will open it to add a global stylesheet, change budgets (size warnings) or point a build at an environment. The ng build default is the production configuration, which minifies and tree-shakes the code.

What the generators write for you

Run ng generate component user-card and the CLI writes four files into src/app/user-card/: user-card.ts, user-card.html, user-card.css and user-card.spec.ts. It chooses the class name (UserCard), the selector (app-user-card) and wires the file names together. A generator is just a template with names filled in.

The first stand-in below does exactly that in a few lines. It is not the CLI's code, only the same idea.

TypeScript A stand-in for ng generate component

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

Change the name to shopping-cart and run it again: the selector becomes app-shopping-cart and the class ShoppingCart. The CLI's naming rules (kebab-case files and selectors, PascalCase classes) are part of the Angular style guide, and generators keep your whole team consistent.

Reading a CLI command

The ng command accepts short aliases and flags. The second stand-in parses a command line the way a CLI front end does: resolve the alias, then separate positional arguments from --flags.

TypeScript A stand-in for parsing an ng command line

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

Note that ng g s means "generate service" while ng s means "serve": the alias depends on the position. Real CLIs have many more rules, but they all start with this kind of resolution step.

Building for production

ng build compiles everything with the application builder (based on esbuild and Vite) and writes static files to dist/my-app/browser. Upload that folder to any static host. The build and deploy lesson covers hosting, and how Angular works explains what the compiler does during the build.

Commit package-lock.json and keep node_modules out of version control (the generated .gitignore already does this). After cloning a project, run npm install once, then ng serve.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does ng serve do?

    ng serve compiles the app and serves it locally, by default at http://localhost:4200, reloading when you save a file.
  2. 2. Which function starts an Angular application from main.ts?

    bootstrapApplication(App, appConfig) creates the root component and renders it where the matching selector appears in index.html.
  3. 3. Where do you usually add an application-wide feature such as the router?

    Features are registered with provideXyz() functions, for example provideRouter(routes), in the providers array of the application config.
  4. 4. What is the purpose of angular.json?

    angular.json defines build targets, entry files, global styles and the development and production configurations. Dependencies live in package.json.

Summary

  • The Angular CLI (ng) creates, serves, builds, generates, tests and upgrades projects; install it with npm install -g @angular/cli on an LTS Node.
  • ng new scaffolds a project, ng serve runs it on port 4200 with live reload, and ng build writes optimized static files to dist.
  • index.html contains <app-root>; main.ts calls bootstrapApplication(App, appConfig); app.config.ts lists the application-wide providers.
  • angular.json configures builds and public/ holds static files. File names differ slightly between versions (app.ts and App in version 20).
  • Generators such as ng generate component fill templates with consistent names; use --dry-run to preview.

Next: Your First Component