| name | debug:angular |
| description | Debug Angular applications systematically with expert-level diagnostic techniques. This skill provides comprehensive guidance for troubleshooting dependency injection errors, change detection issues (NG0100), RxJS subscription leaks, lazy loading failures, zone.js problems, and common Angular runtime errors. Includes structured four-phase debugging methodology, Angular DevTools usage, console debugging utilities (ng.probe), and performance profiling strategies for modern Angular applications. |
Angular Debugging Guide
This guide provides a systematic approach to debugging Angular applications, covering common error patterns, debugging tools, and structured resolution phases.
Common Error Patterns
NullInjectorError
Symptoms:
NullInjectorError: No provider for <ServiceName>
StaticInjectorError: No provider for <ServiceName>
- Service injection fails at runtime
Root Causes:
- Service not provided in any module or component
- Circular dependency between services
- Missing
@Injectable() decorator
- Service provided in wrong scope (lazy-loaded module vs root)
Solutions:
@Injectable({
providedIn: 'root'
})
export class MyService { }
@NgModule({
providers: [MyService]
})
export class FeatureModule { }
@Component({
providers: [MyService]
})
export class MyComponent { }
ExpressionChangedAfterItHasBeenCheckedError (NG0100)
Symptoms:
- Error appears only in development mode
- Typically occurs in
ngAfterViewInit or ngAfterContentInit
- Value changes during change detection cycle
Root Causes:
- Modifying component state in lifecycle hooks after change detection
- Child component modifying parent state
- Async operations completing during change detection
Solutions:
ngAfterViewInit() {
setTimeout(() => {
this.value = 'new value';
});
}
constructor(private cdr: ChangeDetectorRef) {}
ngAfterViewInit() {
this.value = 'new value';
this.cdr.detectChanges();
}
value$ = this.service.getValue();
Common NG Error Codes (NG0100-NG0999)
| Error Code | Description | Common Fix |
|---|
| NG0100 | Expression changed after checked | Use detectChanges() or setTimeout |
| NG0200 | Circular dependency in DI | Refactor service dependencies |
| NG0201 | No provider for service | Add to providers array or use providedIn |
| NG0300 | Multiple components match selector | Make selectors more specific |
| NG0301 | Export not found | Check export name in directive/component |
| NG0302 | Pipe not found | Import module containing pipe |
| NG0303 | No matching element | Check selector syntax |
| NG0500 | Hydration mismatch (SSR) | Ensure server/client render same content |
| NG0910 | Unsafe binding | Sanitize or use bypassSecurityTrust* |
| NG0912 | Component ID collision | Unique component selectors |
RxJS Subscription Leaks
Symptoms:
- Memory leaks in long-running applications
- Console warnings about destroyed components
- Multiple HTTP requests for same data
- Performance degradation over time
Detection:
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
@Component({...})
export class MyComponent {
private destroyRef = inject(DestroyRef);
ngOnInit() {
this.service.getData()
.pipe(takeUntilDestroyed(this.destroyRef))
.subscribe(data => this.data = data);
}
}
private destroy$ = new Subject<void>();
ngOnInit() {
this.service.getData()
.pipe(takeUntil(this.destroy$))
.subscribe();
}
ngOnDestroy() {
this.destroy$.next();
this.destroy$.complete();
}
RxJS Debugging Operators:
import { tap } from 'rxjs/operators';
this.data$.pipe(
tap({
next: v => console.log('Value:', v),
error: e => console.error('Error:', e),
complete: () => console.log('Complete')
})
).subscribe();
Lazy Loading Failures
Symptoms:
ChunkLoadError in console
- Module fails to load on navigation
- Network errors for chunk files
Common Causes:
- Incorrect path in
loadChildren
- Missing default export
- Network/CDN issues
- Cache issues after deployment
Solutions:
const routes: Routes = [
{
path: 'feature',
loadChildren: () => import('./feature/feature.module')
.then(m => m.FeatureModule)
},
{
path: 'standalone',
loadComponent: () => import('./standalone/standalone.component')
.then(c => c.StandaloneComponent)
}
];
constructor(private router: Router) {
this.router.events.subscribe(event => {
if (event instanceof NavigationError) {
if (event.error.name === 'ChunkLoadError') {
window.location.reload();
}
}
});
}
Zone.js Issues
Symptoms:
- Change detection not triggering
- UI not updating after async operations
runOutsideAngular causing update issues
Solutions:
constructor(private ngZone: NgZone) {}
ngOnInit() {
someExternalLibrary.onEvent((data) => {
this.ngZone.run(() => {
this.data = data;
});
});
}
runHeavyComputation() {
this.ngZone.runOutsideAngular(() => {
const result = this.compute();
this.ngZone.run(() => {
this.result = result;
});
});
}
Zoneless Angular (Angular 18+)
import { signal, computed, effect } from '@angular/core';
@Component({
selector: 'app-zoneless',
template: `<p>Count: {{ count() }}</p>`
})
export class ZonelessComponent {
count = signal(0);
doubled = computed(() => this.count() * 2);
increment() {
this.count.update(c => c + 1);
}
}
Debugging Tools
Angular DevTools
Installation:
Features:
- Component Explorer: Inspect component tree, inputs, outputs, and state
- Profiler: Record and analyze change detection cycles
- Dependency Injection Graph: Visualize injector hierarchy
- Router Tree: Debug routing configuration
Requirements:
- Application must be in development mode (
ng serve)
- For deployed apps, build with
optimization: false
ng.probe() (Console Debugging)
ng.getComponent($0);
ng.getDirectives($0);
ng.getOwningComponent($0);
ng.getInjector($0);
ng.applyChanges(component);
ng.getContext($0);
const appRef = ng.getInjector(document.querySelector('app-root'))
.get(ng.coreTokens.ApplicationRef);
appRef.tick();
Source Maps Configuration
{
"projects": {
"my-app": {
"architect": {
"build": {
"configurations": {
"development": {
"sourceMap": true,
"optimization": false,
"extractLicenses": false,
"namedChunks": true
},
"production": {
"sourceMap": {
"scripts": true,
"styles": true,
"hidden": true,
"vendor": false
}
}
}
}
}
}
}
}
Custom Error Handler
import { ErrorHandler, Injectable, Injector } from '@angular/core';
import { HttpErrorResponse } from '@angular/common/http';
@Injectable()
export class GlobalErrorHandler implements ErrorHandler {
constructor(private injector: Injector) {}
handleError(error: Error | HttpErrorResponse): void {
if (error instanceof HttpErrorResponse) {
console.error('HTTP Error:', error.status, error.message);
} else {
console.error('Client Error:', error.message);
console.error('Stack:', error.stack);
}
const loggingService = this.injector.get(LoggingService);
loggingService.logError(error);
}
}
@NgModule({
providers: [
{ provide: ErrorHandler, useClass: GlobalErrorHandler }
]
})
export class AppModule { }
HTTP Interceptor for Debugging
@Injectable()
export class DebugInterceptor implements HttpInterceptor {
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const started = Date.now();
return next.handle(req).pipe(
tap({
next: (event) => {
if (event instanceof HttpResponse) {
const elapsed = Date.now() - started;
console.log(`${req.method} ${req.urlWithParams} - ${elapsed}ms`);
}
},
error: (error) => {
console.error(`${req.method} ${req.urlWithParams} - Error:`, error);
}
})
);
}
}
The Four Phases of Angular Debugging
Phase 1: Reproduce and Isolate
Objective: Consistently reproduce the issue and narrow down the scope.
Steps:
- Reproduce the error - Get exact steps to trigger the issue
- Check the console - Note full error message and stack trace
- Identify the component - Which component/service is affected?
- Check recent changes - Use
git diff to see what changed
Commands:
git diff HEAD~5 --name-only
ng build --configuration development 2>&1 | head -50
ng lint
Console Investigation:
console.group('Component State');
console.log('Inputs:', this.inputValue);
console.log('State:', this.state);
console.trace('Call stack');
console.groupEnd();
Phase 2: Analyze the Error
Objective: Understand exactly what is failing and why.
For DI Errors:
const injector = TestBed.inject(Injector);
console.log('Providers:', injector);
try {
const service = TestBed.inject(MyService);
console.log('Service found:', service);
} catch (e) {
console.error('Service not found:', e);
}
For Change Detection Errors:
import { enableDebugTools } from '@angular/platform-browser';
platformBrowserDynamic().bootstrapModule(AppModule)
.then(ref => {
const appRef = ref.injector.get(ApplicationRef);
const componentRef = appRef.components[0];
enableDebugTools(componentRef);
});
For Template Errors:
{{ user?.profile?.name }}
@if (user) {
<p>{{ user.name }}</p>
} @else {
<p>Loading...</p>
}
<p *ngIf="user; else loading">{{ user.name }}</p>
<ng-template #loading>Loading...</ng-template>
Phase 3: Apply the Fix
Objective: Implement and verify the solution.
Fix Patterns:
this.data$ = this.service.getData().pipe(
catchError(error => {
console.error('Data fetch failed:', error);
return of(null);
}),
shareReplay(1)
);
@Input() set items(value: Item[]) {
this._items = value ?? [];
this.updateView();
}
private _items: Item[] = [];
@Component({
changeDetection: ChangeDetectionStrategy.OnPush
})
export class OptimizedComponent {
@Input() data!: ReadonlyArray<Item>;
updateData() {
this.data = [...this.data, newItem];
}
}
Verify the Fix:
ng test --include="**/affected.component.spec.ts"
ng e2e --spec="affected.e2e-spec.ts"
ng build --configuration production
Phase 4: Prevent Regression
Objective: Add tests and monitoring to prevent recurrence.
Write Targeted Tests:
describe('BugfixComponent', () => {
it('should handle null input gracefully', () => {
component.data = null;
fixture.detectChanges();
expect(component.displayData).toEqual([]);
});
it('should unsubscribe on destroy', () => {
const subscription = component['subscription'];
spyOn(subscription, 'unsubscribe');
component.ngOnDestroy();
expect(subscription.unsubscribe).toHaveBeenCalled();
});
it('should handle HTTP errors', fakeAsync(() => {
spyOn(service, 'getData').and.returnValue(
throwError(() => new Error('Network error'))
);
component.loadData();
tick();
expect(component.error).toBe('Failed to load data');
}));
});
Add Error Boundary:
@Component({
selector: 'app-error-boundary',
template: `
@if (hasError) {
<div class="error-fallback">
<h2>Something went wrong</h2>
<button (click)="retry()">Retry</button>
</div>
} @else {
<ng-content></ng-content>
}
`
})
export class ErrorBoundaryComponent implements OnInit, ErrorHandler {
hasError = false;
handleError(error: Error): void {
this.hasError = true;
console.error('Error caught:', error);
}
retry(): void {
this.hasError = false;
}
}
Quick Reference Commands
Development
ng serve
ng serve --configuration=development
ng serve --ssl
ng serve --open
ng serve --port 4201
ng serve --host 0.0.0.0 --disable-host-check
Building
ng build --configuration development
ng build --configuration production
ng build --stats-json
npx webpack-bundle-analyzer dist/my-app/stats.json
Testing
ng test
ng test --no-watch --code-coverage
ng test --include="**/my.component.spec.ts"
ng test --browsers=ChromeHeadless
ng test --browsers=Chrome
ng e2e
Linting and Code Quality
ng lint
ng lint --fix
ng lint --files="src/app/my.component.ts"
Generating Code
ng generate component my-component
ng generate service my-service
ng generate module my-module --routing
ng generate component my-component --dry-run
Cache Management
ng cache clean
npm cache clean --force
rm -rf node_modules package-lock.json
npm install
Debugging Specific Issues
ng version
ng update @angular/core @angular/cli
npm outdated
npx tsc --showConfig
npx madge --circular src/
node --inspect node_modules/.bin/ng serve
Performance Debugging
Profiling Change Detection
import { enableProdMode } from '@angular/core';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
platformBrowserDynamic().bootstrapModule(AppModule)
.then(moduleRef => {
const appRef = moduleRef.injector.get(ApplicationRef);
const componentRef = appRef.components[0];
enableDebugTools(componentRef);
});
OnPush Optimization
@Component({
changeDetection: ChangeDetectionStrategy.OnPush
})
export class PerformantComponent {
count = signal(0);
doubled = computed(() => this.count() * 2);
data$ = this.service.getData().pipe(
shareReplay({ bufferSize: 1, refCount: true })
);
}
TrackBy for Lists
@Component({
template: `
@for (item of items; track item.id) {
<app-item [item]="item" />
}
`
})
export class ListComponent {
items: Item[] = [];
trackById(index: number, item: Item): number {
return item.id;
}
}
Resources