Skip to content

How to Fix Angular NullInjectorError

DodaTech Updated 2026-06-24 3 min read

In this tutorial, you'll learn about How to Fix Angular NullInjectorError. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.

The Problem

Angular throws NullInjectorError: No provider for ServiceName when a component, directive, or pipe tries to inject a service that has not been provided to the Dependency Injection system.

Quick Fix

Step 1: Add @Injectable with providedIn

The recommended approach is to use providedIn: 'root':

export class DataService {
    fetchData() {
        return fetch('/api/data');
    }
}

Add the decorator:

import { Injectable } from '@angular/core';

@Injectable({
    providedIn: 'root'
})
export class DataService {
    fetchData() {
        return fetch('/api/data');
    }
}

This makes the service available application-wide without adding it to any module.

Step 2: Add the service to NgModule providers

For services in a feature module:

@NgModule({
    providers: [DataService]
})
export class FeatureModule { }

Or for a specific component:

@Component({
    selector: 'app-example',
    template: '...',
    providers: [DataService]
})
export class ExampleComponent {
    constructor(private dataService: DataService) {}
}

Step 3: Check module imports

A service from a third-party module requires the module to be imported:

@NgModule({
    imports: [
        HttpClientModule, // Required for HttpClient
        FormsModule      // Required for ngModel
    ]
})
export class AppModule { }

Step 4: Check for lazy-loaded module isolation

Each lazy-loaded module has its own injector. A service provided in a lazy module is not available to the root application:

// In the lazy module
@NgModule({
    providers: [LazyService]
})
export class LazyModule { }

To share, provide at root level:

@Injectable({ providedIn: 'root' })
export class LazyService { }

Step 5: Check constructor parameter order

Angular resolves dependencies by type. An incorrect import path can cause resolution failure:

import { HttpClient } from '@angular/common/http'; // Correct

Verify the import path matches the Angular package.

Prevention

  • Always add @Injectable({ providedIn: 'root' }) to services.
  • Use Angular CLI to generate services: ng generate service data.
  • Avoid providing services in multiple places unless intentionally scoped.
  • Use providedIn: 'root' for Singleton services.
  • Check module imports when using Angular built-in services.

Common Mistakes with null injector

  1. Using return to exit a function early instead of wrapping a pure value in the monad
  2. Mixing let bindings with <- bindings in do notation, producing type errors
  3. Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors

These mistakes appear frequently in real-world Angular code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### What is the difference between providedIn: 'root' and adding to module providers?

providedIn: 'root' makes the service a Singleton available everywhere and allows tree-shaking (if the service is not used, it is removed from the bundle). Adding to module providers creates a service instance scoped to that module's injector, which may create multiple instances if used in multiple modules.

Why does my service work in development but not in production?

This is usually a tree-shaking issue. Services provided in module providers but unused in the module's components may be removed by the optimizer. Use providedIn: 'root' or ensure the service is referenced somewhere in the module's component tree.

Can I inject a service without @Injectable?

No. The @Injectable decorator is required for Angular to generate the metadata needed for Dependency Injection. Even if the service has no dependencies itself, the decorator is mandatory since Angular 6+. Angular CLI automatically adds it when generating services.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro