0
0
NestjsHow-ToBeginner ยท 3 min read

How to Inject Service in NestJS: Simple Guide with Examples

In NestJS, you inject a service by marking it with @Injectable() and then adding it as a parameter in the constructor of the class where you want to use it. NestJS automatically provides the service instance via dependency injection when the class is instantiated.
๐Ÿ“

Syntax

To inject a service in NestJS, first decorate the service class with @Injectable(). Then, in the class where you want to use the service, add it as a parameter in the constructor. NestJS will inject the service instance automatically.

  • @Injectable(): Marks a class as a provider that can be injected.
  • Constructor parameter: The service to inject is declared here.
typescript
import { Injectable } from '@nestjs/common';

@Injectable()
export class MyService {
  getHello(): string {
    return 'Hello from MyService!';
  }
}

export class MyController {
  constructor(private readonly myService: MyService) {}

  greet(): string {
    return this.myService.getHello();
  }
}
๐Ÿ’ป

Example

This example shows a simple service GreetingService injected into a controller AppController. The controller calls a method from the service to return a greeting message.

typescript
import { Injectable, Controller, Get } from '@nestjs/common';

@Injectable()
export class GreetingService {
  getGreeting(): string {
    return 'Hello, NestJS!';
  }
}

@Controller()
export class AppController {
  constructor(private readonly greetingService: GreetingService) {}

  @Get()
  getHello(): string {
    return this.greetingService.getGreeting();
  }
}
Output
When you visit the root route, the response is: Hello, NestJS!
โš ๏ธ

Common Pitfalls

Common mistakes when injecting services in NestJS include:

  • Not adding @Injectable() to the service class, so NestJS cannot inject it.
  • Forgetting to register the service in a module's providers array.
  • Using private fields without readonly can cause unintended mutations.
  • Trying to inject a service that is not provided in the current module or imported modules.
typescript
/* Wrong: Missing @Injectable decorator */
export class WrongService {}

/* Right: Add @Injectable and register in module */
import { Injectable, Module } from '@nestjs/common';

@Injectable()
export class RightService {}

// In your module
@Module({
  providers: [RightService],
})
export class AppModule {}
๐Ÿ“Š

Quick Reference

StepDescription
1. Create ServiceDefine a class and decorate with @Injectable()
2. Register ServiceAdd the service to the module's providers array
3. Inject ServiceAdd the service as a constructor parameter where needed
4. Use ServiceCall service methods via the injected instance
โœ…

Key Takeaways

Always decorate services with @Injectable() to enable injection.
Inject services by adding them as constructor parameters with private readonly.
Register services in the module's providers array to make them available.
NestJS handles creating and providing service instances automatically.
Avoid common mistakes like missing decorators or not registering providers.