← Back to Angular, page 3

angular-0055 · Easy fresher

Which Angular CLI commands do you use every day, and what files does ng generate component create?

The CLI creates projects (ng new), generates code (ng generate), runs a dev server (ng serve), builds for production (ng build), runs tests (ng test), and upgrades versions (ng update). ng generate component makes a class, a template, a style file, and a spec, using the project's defaults from angular.json.

Full answer

The Angular CLI is the ng command. The commands you will use most:

  • ng new shop creates a project with the recommended setup.
  • ng serve starts a development server with live reload.
  • ng generate component student-card (short form ng g c student-card) creates files. Other generators: service, directive, pipe, guard, interceptor, resolver, class, interface, enum.
  • ng build builds the app. The default configuration is production, which minifies, tree-shakes, and adds hashes to file names. Use ng build --configuration development for an unoptimised build with source maps.
  • ng test runs unit tests, and ng e2e runs end-to-end tests if a tool is configured.
  • ng update upgrades Angular packages with migration scripts. ng add installs a package and runs its setup, for example ng add @angular/material.
  • ng version and ng config show or change settings.

What ng generate component student-card creates in Angular 20: student-card.ts (the class), student-card.html, student-card.css, and student-card.spec.ts. The component is standalone, and the class is named StudentCard without the old Component suffix. Older projects (and the older CLI) create student-card.component.ts and a class named StudentCardComponent. Both are valid. Flags such as --inline-template, --inline-style, --skip-tests, and --change-detection OnPush change the output. Defaults can be set in angular.json under schematics.

Key files in a project: angular.json (build, serve, and test settings), tsconfig.json (TypeScript options), package.json, src/main.ts (bootstrap), src/app/app.config.ts (providers), src/app/app.routes.ts (routes), and src/index.html.

The CLI is also what makes upgrades predictable. ng update moves code with schematics, so read its messages and commit before running it.

typescript
// Commands:
//   ng new shop
//   ng generate component student-card
//   ng serve
//   ng test
//   ng build

// file: student-card.ts
import { Component } from '@angular/core';

@Component({
  selector: 'app-student-card',
  imports: [],
  templateUrl: './student-card.html',
  styleUrl: './student-card.css',
})
export class StudentCard {}

// file: student-card.html
<p>student-card works!</p>

// file: student-card.css
/* empty file */

// file: student-card.spec.ts
import { ComponentFixture, TestBed } from '@angular/core/testing';

import { StudentCard } from './student-card';

describe('StudentCard', () => {
  let component: StudentCard;
  let fixture: ComponentFixture<StudentCard>;

  beforeEach(async () => {
    await TestBed.configureTestingModule({
      imports: [StudentCard],
    }).compileComponents();

    fixture = TestBed.createComponent(StudentCard);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

  it('should create', () => {
    expect(component).toBeTruthy();
  });
});

The files below are the real output of `ng generate component student-card` in a fresh Angular 20.3 project (the `.css` file is empty). The spec was run with `ng test` in headless Chrome and passes. The commands are listed in the first comment.

Key points

  • `ng new`, `serve`, `generate`, `build`, `test`, `update`, `add` cover daily work.
  • `ng generate component` creates a class, template, style, and spec file.
  • Angular 20 names the class without the `Component` suffix. Older code uses it. Both work.
  • `angular.json` holds the build and generator settings.

Common mistakes

  • Hand-writing boilerplate that the generators create with correct defaults.
  • Running `ng update` with uncommitted changes, which makes the migration diff hard to read.
  • Confusing `ng add` (installs and configures) with `npm install` (only installs).

Follow-ups

  • What is the difference between `ng build` configurations?
  • How do you change the defaults of `ng generate` for the whole project?
  • What does `ng update` actually do?

Tips

  • Mention that generated names changed in Angular 20, so you can read both styles.
  • Say how you would use `ng update` safely: clean working tree, small steps.
  • Know where the settings live (`angular.json`).

Evaluation rubric

  • Lists the main commands with their purpose.
  • Knows what the component generator creates.
  • Understands the project's key files.