Skip to content

Setup and Installation

Before you start, ensure you have the following installed:

  • Node.js (v18.19 or higher)
  • npm (v9 or higher) or yarn
  • A code editor (VS Code recommended)
  • Basic knowledge of TypeScript and JavaScript

Visit nodejs.org and download the LTS (Long-Term Support) version.

After installation, verify:

Terminal window
node --version # Should be v18.19.0 or higher
npm --version # Should be v9.0.0 or higher

The Angular CLI is the official command-line tool for creating and managing Angular projects.

Terminal window
npm install -g @angular/cli@latest

Verify the installation:

Terminal window
ng version

You should see output similar to:

_ _ ____ _ ___
/ \ _ __ __ _ _ _| | __ _ _ __ / ___| | |_ _|
/ △ \ | '_ \ / _` | | | | |/ _` | '__| | | | | | |
/ ___ \ | | | | (_| | | | | | (_| | | | |___| |___ | |
/_/ \_\|_| |_|\__, |_|_|_|_|\__,_|_| \____|_____|___|
|___/
​
Angular CLI: 18.0.0
Node: 18.19.0
Package Manager: npm 10.2.3

The Angular CLI provides an interactive setup for creating new projects:

Terminal window
ng new my-app

The CLI will prompt you with several questions:

? Which stylesheet format would you like to use?
> CSS
SCSS (recommended for large projects)
SASS
LESS
? Do you want to enable Server-Side Rendering (SSR)?
> Yes
No (recommended for most projects)
? Do you want to add Angular routing?
> Yes (recommended)
No

After creation, your project will look like:

my-app/
├── src/
│ ├── app/
│ │ ├── app.component.ts
│ │ ├── app.component.html
│ │ ├── app.component.scss
│ │ ├── app.component.spec.ts
│ │ └── app.routes.ts
│ ├── main.ts # Application entry point
│ ├── styles.scss # Global styles
│ └── index.html # HTML template
├── angular.json # Angular CLI configuration
├── tsconfig.json # TypeScript configuration
├── package.json # Dependencies
└── README.md
Terminal window
cd my-app
Terminal window
ng serve

Or with automatic browser opening:

Terminal window
ng serve --open

The application will be available at http://localhost:4200/

Understanding the Development Server Output

Section titled “Understanding the Development Server Output”
✔ Browser application bundle generation complete.
Initial Chunk Files | Names | Raw Size | Estimated Gzip
main.js | main | 234 kB | 68 kB
polyfills.js | polyfills | 124 kB | 39 kB
styles.css | styles | 25 kB | 6 kB
Build at: 2024-05-11T10:30:45.123Z - Hash: abc123def456
Application bundle generation complete. [4.234 seconds]
Watch mode enabled. Watching for file changes...

Open src/app/app.component.ts:

import { Component } from '@angular/core';
@Component({
selector: 'app-root',
standalone: true,
template: `
<h1>Welcome to {{ title }}!</h1>
`,
styles: [],
})
export class AppComponent {
title = 'my-app';
}

Edit the template to see changes instantly:

template: `
<h1>Hello, Angular 18!</h1>
<p>Welcome to your first Angular application</p>
`

Save the file and watch the browser reload automatically.

Section titled “With SCSS (Recommended for Large Projects)”
Terminal window
ng new my-app --style=scss
Terminal window
ng new my-app --routing
Terminal window
ng new my-app \
--skip-git \
--style=scss \
--routing \
--package-manager=npm
Terminal window
ng new my-app --skip-install
cd my-app
npm install

Once your project is created, you have several useful commands:

Terminal window
# Start development server
ng serve
# With auto-open browser
ng serve --open
# On specific port
ng serve --port 4300

For a better development experience in VS Code:

  1. Angular Language Service — Official extension by Angular
  2. Prettier — Code formatter
  3. ESLint — Linting support
  4. TypeScript Vue Plugin (Volar) — Better TypeScript support

Install them via the Extensions marketplace.

Angular supports multiple environments. Edit angular.json to add configurations:

"configurations": {
"development": {
"budgets": [...],
"namedChunks": true
},
"production": {
"budgets": [...],
"aot": true,
"outputHashing": "all"
},
"staging": {
"budgets": [...],
"aot": true
}
}

Build for different environments:

Terminal window
ng build --configuration production
ng build --configuration staging

If you work on multiple projects with different Node.js version requirements, NVM (Node Version Manager) allows you to easily switch between versions.

Terminal window
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash

After installation, close and reopen your terminal, then verify:

Terminal window
nvm --version # Should display version number

Use nvm-windows from: https://github.com/coreybutler/nvm-windows/releases

Download the installer and follow the installation instructions.

Terminal window
nvm list-remote # Show all available versions
Terminal window
nvm install 18.19.0 # Install specific version
nvm install 20.11.0 # Install another version
nvm install --lts # Install latest LTS version
Terminal window
nvm list # or `nvm ls`

Output example:

v16.13.0
v18.19.0
-> v20.11.0 (current)
v21.5.0
Terminal window
nvm use 18.19.0 # Use specific version for current terminal session
nvm use --lts # Use latest LTS version
Terminal window
nvm alias default 18.19.0

Create a .nvmrc file in your project root:

18.19.0

Then automatically use the correct version:

Terminal window
nvm use # Reads .nvmrc and switches to specified version
Terminal window
# Project 1 (Angular 18 - needs Node 18)
cd my-angular-app
nvm use 18.19.0
ng serve
# Project 2 (Node backend - needs Node 20)
cd ../my-api
nvm use 20.11.0
npm start

Symptoms:

bash: ng: command not found

Causes:

  • Angular CLI not installed globally
  • npm global bin directory not in PATH
  • Installation failed silently

Solutions:

  1. Install Angular CLI globally:
Terminal window
npm install -g @angular/cli@latest
  1. Verify npm global directory:
Terminal window
npm config get prefix
  1. If using nvm, reinstall global packages after switching versions:
Terminal window
npm install -g @angular/cli@latest
  1. Use npx instead (no global installation needed):
Terminal window
npx @angular/cli@latest serve
npx @angular/cli@latest generate component my-component

Symptoms:

Error: Port 4200 is already in use. Would you like to use a different port (4201)?

Causes:

  • Another application using the port
  • Previous Angular server still running
  • Multiple development servers started

Solutions:

  1. Use a different port:
Terminal window
ng serve --port 4300
  1. Kill the process using port 4200:

macOS/Linux:

Terminal window
lsof -ti:4200 | xargs kill -9

Windows (PowerShell):

Terminal window
Stop-Process -Id (Get-NetTCPConnection -LocalPort 4200).OwningProcess -Force
  1. Check what’s using the port:

macOS/Linux:

Terminal window
lsof -i :4200

Windows (Command Prompt):

Terminal window
netstat -ano | findstr :4200

Symptoms:

The engine "node" is incompatible with this module.
Expected version ">=18.19.0". Got "16.13.0"

Causes:

  • Node.js version too old
  • Project requires specific Node version
  • Version mismatch between projects

Solutions:

  1. Check current Node version:
Terminal window
node --version
  1. Update Node.js directly:

    • Visit nodejs.org and download the LTS version
    • Run the installer
  2. Use NVM for version management (Recommended):

Terminal window
nvm install 18.19.0
nvm use 18.19.0
node --version # Verify the change
  1. Create .nvmrc in project root:
18.19.0

Then use:

Terminal window
nvm use

Symptoms:

npm ERR! code ERESOLVE
npm ERR! ERESOLVE unable to resolve dependency tree

Causes:

  • Corrupted npm cache
  • Conflicting dependencies
  • Network issues
  • npm configuration issues

Solutions:

  1. Clear npm cache:
Terminal window
npm cache clean --force
npm install
  1. Try npm install with legacy peer deps:
Terminal window
npm install --legacy-peer-deps
  1. Delete lock file and reinstall:
Terminal window
rm package-lock.json
npm install
  1. Use npm ci (Recommended for clean installs):
Terminal window
npm ci # Installs exact versions from package-lock.json
  1. Update npm itself:
Terminal window
npm install -g npm@latest

Symptoms:

error TS2304: Cannot find name 'NgModule'
error TS1005: ',' expected

Causes:

  • TypeScript version mismatch
  • Missing type definitions
  • Incorrect tsconfig.json
  • Angular packages not installed

Solutions:

  1. Reinstall Angular dependencies:
Terminal window
rm node_modules package-lock.json
npm install
  1. Update Angular and related packages:
Terminal window
ng update @angular/core @angular/cli
  1. Check TypeScript version:
Terminal window
npx tsc --version
  1. Rebuild the project:
Terminal window
ng build

Symptoms:

Access to XMLHttpRequest at 'http://api.example.com/data'
from origin 'http://localhost:4200' has been blocked by CORS policy

Causes:

  • Backend API doesn’t allow requests from localhost:4200
  • Missing CORS headers on backend
  • Incorrect proxy configuration

Solutions:

  1. Use proxy configuration in development:

Create/edit proxy.conf.json:

{
"/api": {
"target": "http://localhost:3000",
"secure": false,
"pathRewrite": {
"^/api": ""
}
}
}
  1. Update angular.json:
"serve": {
"options": {
"proxyConfig": "proxy.conf.json"
}
}
  1. Use the proxy in your API calls:
// Instead of: http://localhost:3000/api/users
this.http.get('/api/users')

Symptoms:

JavaScript heap out of memory
FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed

Causes:

  • Large project with many dependencies
  • Default Node memory limit too low
  • Inefficient build configuration

Solutions:

  1. Increase Node memory:
Terminal window
export NODE_OPTIONS=--max_old_space_size=4096
ng build
  1. For Windows (Command Prompt):
Terminal window
set NODE_OPTIONS=--max_old_space_size=4096
ng build
  1. For Windows (PowerShell):
Terminal window
$env:NODE_OPTIONS="--max_old_space_size=4096"
ng build
  1. Add to npm scripts in package.json:
{
"scripts": {
"build": "export NODE_OPTIONS=--max_old_space_size=4096 && ng build"
}
}

Symptoms:

error TS2307: Cannot find module '@angular/core' or its corresponding type declarations

Causes:

  • Missing node_modules directory
  • Incomplete npm installation
  • Package.json missing dependencies
  • Incorrect import paths

Solutions:

  1. Reinstall dependencies:
Terminal window
npm install
  1. Check if package is in package.json:
Terminal window
grep "@angular/core" package.json
  1. Install missing packages:
Terminal window
npm install @angular/core --save
  1. Verify import paths use correct aliases:
// Good
import { MyComponent } from '@app/components/my-component';
// Bad (if path alias not configured)
import { MyComponent } from '../../../components/my-component';

Find and kill processes by port (All platforms):

Terminal window
# Use this npm package (works everywhere)
npm install -g kill-port
kill-port 4200

Or create a utility script in package.json:

{
"scripts": {
"kill:4200": "kill-port 4200",
"serve": "npm run kill:4200 && ng serve"
}
}
Terminal window
ng serve --verbose

This provides detailed information about the build process and can help identify issues.

Terminal window
ng version --verbose

Shows detailed Angular and system information useful for debugging.

Terminal window
# Skip package manager installation
ng new my-app --skip-install
# Use specific package manager
ng new my-app --package-manager=npm
# Skip git initialization
ng new my-app --skip-git
# Use specific Angular version
ng new my-app --package-manager npm --version 17

Once your development environment is set up:

  1. ✅ Create your first component
  2. ✅ Understand Angular’s component architecture
  3. ✅ Learn about services and dependency injection
  4. ✅ Explore routing and navigation
  5. ✅ Master forms and form validation
  6. ✅ Add HTTP communication with your backend