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
providersarray. - Using private fields without
readonlycan 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
| Step | Description |
|---|---|
| 1. Create Service | Define a class and decorate with @Injectable() |
| 2. Register Service | Add the service to the module's providers array |
| 3. Inject Service | Add the service as a constructor parameter where needed |
| 4. Use Service | Call 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.