Sub topic 5
Touring the Generated Project
ng new generates a few dozen files. Most of them stay the same for the life of a project. A few you will edit every day. This chapter goes through the workspace from the top down: what each file does, which ones you change, and which ones you can usually leave alone.
This chapter assumes you have already run ng new my-app as in the previous chapter. It also assumes you can read JSON and basic TypeScript, and that you know what npm install does.
The Workspace at a Glance
Run this from the folder that contains my-app to list the tree without the dependency folder:
cd my-app
find . -path ./node_modules -prune -o -path ./.git -prune -o -type f -print | sortA project created with a recent CLI (v20 or later, no server-side rendering) looks roughly like this:
.
├── .editorconfig
├── .gitignore
├── .vscode/
│ ├── extensions.json
│ ├── launch.json
│ └── tasks.json
├── README.md
├── angular.json
├── package.json
├── package-lock.json
├── public/
│ └── favicon.ico
├── src/
│ ├── index.html
│ ├── main.ts
│ ├── styles.css
│ └── app/
│ ├── app.config.ts
│ ├── app.routes.ts
│ ├── app.ts
│ ├── app.html
│ ├── app.css
│ └── app.spec.ts
├── tsconfig.json
├── tsconfig.app.json
└── tsconfig.spec.jsonThe CLI's terms are worth learning now. A workspace is the whole folder, configured by angular.json. A workspace can hold one or more projects. By default ng new creates a single application project whose source sits in src/ at the workspace root.
Workspace-Level Configuration Files
| File | Purpose | Edit it? |
|---|---|---|
| `angular.json` | Build, serve and test settings for each project | Occasionally |
| `package.json` | npm dependencies and scripts | Yes, via `npm install` or `ng add` |
| `package-lock.json` | Exact resolved dependency versions | Never by hand; commit it |
| `tsconfig.json` | Base TypeScript compiler options | Rarely |
| `tsconfig.app.json` | TypeScript settings for application code | Rarely |
| `tsconfig.spec.json` | TypeScript settings for test files | Rarely |
| `.editorconfig` | Indentation and charset rules for editors | Optional |
| `.gitignore` | Keeps `node_modules`, `dist` and caches out of Git | Rarely |
| `.vscode/` | Recommended extensions and debug launch settings for VS Code | Optional |
angular.json
angular.json tells the CLI how to run each command for each project. The architect section maps a target name such as build, serve or test to a builder, which is the package that does the work, along with that builder's options. Here is a trimmed excerpt:
{
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
"version": 1,
"newProjectRoot": "projects",
"projects": {
"my-app": {
"projectType": "application",
"root": "",
"sourceRoot": "src",
"prefix": "app",
"architect": {
"build": {
"builder": "@angular/build:application",
"options": {
"browser": "src/main.ts",
"tsConfig": "tsconfig.app.json",
"assets": [{ "glob": "**/*", "input": "public" }],
"styles": ["src/styles.css"]
},
"configurations": {
"production": { "outputHashing": "all" },
"development": { "optimization": false, "sourceMap": true }
},
"defaultConfiguration": "production"
},
"serve": {
"builder": "@angular/build:dev-server",
"configurations": {
"production": { "buildTarget": "my-app:build:production" },
"development": { "buildTarget": "my-app:build:development" }
},
"defaultConfiguration": "development"
}
}
}
}
}These are the keys you will touch most:
browsernames the entry file. The build starts fromsrc/main.tsand follows its imports.styleslists global stylesheets. A third-party CSS file such as a UI kit's theme goes in here.assetscopies files frompublic/into the build output unchanged.prefixsets the selector prefix the generator uses, which is why components come out asapp-something.configurationsdefine named variants.ng buildusesproductionby default andng serveusesdevelopment.
The tsconfig trio
tsconfig.json holds the shared compiler options. In new projects these include strict: true, plus an angularCompilerOptions block that turns on strict template type checking. The other two files extend it. tsconfig.app.json covers application code and excludes *.spec.ts files. tsconfig.spec.json includes the spec files and adds the test runner's type definitions. This split keeps test-only globals such as describe and expect from type-checking inside production code.
Strict mode explains most "why does my template not compile?" surprises. For example, binding to a property that might be undefined fails at build time instead of at runtime. Leave strict mode on. It catches real bugs.
The src Folder: Your Application
index.html
This is the single HTML page the browser loads:
<!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">
<link rel="icon" type="image/x-icon" href="favicon.ico">
</head>
<body>
<app-root></app-root>
</body>
</html>The file has no <script> tags. The build injects the compiled bundles for you. The <app-root> element is a placeholder that Angular replaces with the root component. The <base href="/"> tag tells the router how to resolve URLs. If you deploy under a sub-path, change it with ng build --base-href /my-sub-path/ instead of editing the file.
main.ts and styles.css
main.ts is the entry point. It calls bootstrapApplication with the root component and the app config. The next chapter covers it line by line. styles.css is the global stylesheet. Rules in it apply to the whole page, unlike component styles, which are scoped to their own component.
The app folder
src/app/ holds the root component and the app-wide configuration:
app.tsis the root component class.app.htmlandapp.cssare its template and its scoped styles.app.spec.tsis its unit test.app.config.tsexports anApplicationConfigobject that holds app-wide providers such as the router.app.routes.tsexports an emptyRoutesarray for you to fill.
The root component as generated:
import { Component, signal } from '@angular/core';
import { RouterOutlet } from '@angular/router';
@Component({
selector: 'app-root',
imports: [RouterOutlet],
templateUrl: './app.html',
styleUrl: './app.css'
})
export class App {
protected readonly title = signal('my-app');
}The selector value app-root matches the element in index.html. That match is the link between the static page and your component tree. The imports array lists RouterOutlet because the template uses <router-outlet>. This is the standalone component model: each component declares its own dependencies, and no NgModule file is involved.
The public Folder
Files in public/ are copied as-is to the root of the build output. A file at public/logo.svg is served at /logo.svg, so you reference it as src="logo.svg" or src="/logo.svg". You don't write public/logo.svg. That extra path segment is a common mistake, and the result is a 404.
How the Pieces Connect
Files You Should Not Edit or Commit
node_modules/is rebuilt bynpm install. It is already in.gitignore.dist/is the output ofng build. Anything you edit there is overwritten on the next build..angular/cache/holds the build cache. Deleting it is safe and sometimes fixes odd stale-build behavior.package-lock.jsonshould be committed, never hand-edited. Usenpm ciin CI so that builds stay reproducible.
Common Mistakes
| Symptom | Likely cause |
|---|---|
| Global CSS from a library has no effect | It isn't listed in `styles` in `angular.json`, or the server wasn't restarted |
| Image returns 404 | The path includes `public/`, or the file sits in an old `src/assets` folder that isn't configured |
| Blank page, console says `app-root` is not a known element | The selector in `app.ts` was renamed but `index.html` still uses the old one |
| `describe` not found in an app file | A test helper was imported into application code, which `tsconfig.app.json` excludes |
| Tutorial says `app.component.ts`, but you have `app.ts` | Your CLI is v20 or later, which drops the suffix by default |
Exercise
You add npm install bootstrap to your project and want its CSS (node_modules/bootstrap/dist/css/bootstrap.min.css) applied to every page. Which file do you change, what exactly do you add, and what must you do afterward for it to show up during development?
Show answer
Edit angular.json and add "node_modules/bootstrap/dist/css/bootstrap.min.css" to the build target's "styles" array, usually before "src/styles.css" so your own rules can override it. Then stop and restart ng serve, because changes to angular.json are not picked up by a running dev server.
Exercise
A teammate puts team-photo.jpg in the public folder and writes <img src="public/team-photo.jpg"> in a template. The image is broken. Why, and what is the fix?
Show answer
Files in public/ are copied to the root of the build output, so the folder name is not part of the URL. Use src="team-photo.jpg" (or "/team-photo.jpg").
Exercise
Why does the workspace have separate tsconfig.app.json and tsconfig.spec.json files instead of one tsconfig.json?
Show answer
Both extend the shared base config, but they include different files and types. The app config excludes *.spec.ts and test-runner types, so test globals can't leak into production code. The spec config includes the test files and adds the test framework's type definitions.