How to Create a Service in NestJS: Simple Guide
In NestJS, create a service by defining a class with the
@Injectable() decorator and export it. Then, inject this service into a controller or another provider using constructor injection with private readonly access modifier.Syntax
To create a service in NestJS, define a class decorated with @Injectable(). This decorator marks the class as a provider that can be injected. Use constructor injection to use the service in other parts of your app.
@Injectable(): Marks the class as a service provider.- Class name: Usually ends with
Service. - Constructor injection: Use
private readonlyto inject the service.
typescript
import { Injectable } from '@nestjs/common'; @Injectable() export class MyService { // service methods here } // Injecting service in a controller import { Controller } from '@nestjs/common'; import { MyService } from './my.service'; @Controller() export class MyController { constructor(private readonly myService: MyService) {} }
Example
This example shows a simple GreetingService that returns a greeting message. The service is injected into a controller to handle a route that returns the greeting.
typescript
import { Injectable, Controller, Get } from '@nestjs/common'; @Injectable() export class GreetingService { getGreeting(): string { return 'Hello from GreetingService!'; } } @Controller('greet') export class GreetingController { constructor(private readonly greetingService: GreetingService) {} @Get() greet(): string { return this.greetingService.getGreeting(); } }
Output
When you visit the /greet route, the response is: Hello from GreetingService!
Common Pitfalls
Common mistakes when creating services in NestJS include:
- Forgetting the
@Injectable()decorator, which prevents NestJS from recognizing the class as a provider. - Not adding the service to the module's
providersarray, so it won't be available for injection. - Using public constructor parameters without
privateorreadonly, which means the service won't be assigned to a class property.
typescript
/* Wrong: Missing @Injectable decorator */ export class WrongService { getData() { return 'data'; } } /* Right: With @Injectable and added to module providers */ import { Injectable } from '@nestjs/common'; @Injectable() export class RightService { getData() { return 'data'; } } // In your module import { Module } from '@nestjs/common'; import { RightService } from './right.service'; @Module({ providers: [RightService], exports: [RightService] }) export class AppModule {}
Quick Reference
Remember these key points when creating services in NestJS:
- Always use
@Injectable()on service classes. - Register services in the module's
providersarray. - Inject services using constructor injection with
private readonly. - Services contain business logic and can be reused across controllers and other providers.
Key Takeaways
Use @Injectable() to mark a class as a NestJS service provider.
Add your service to the module's providers array to enable injection.
Inject services via constructor with private readonly for proper access.
Services hold reusable business logic separate from controllers.
Forgetting @Injectable() or module registration causes injection errors.