@Injectable()
Marks a class as a provider that can be managed and injected by the Nest IoC container.
Syntax
@Injectable(options?: InjectableOptions) Parameters
| Name | Type | Required | Description |
|---|---|---|---|
options | InjectableOptions | No | Optional object with a scope (DEFAULT, REQUEST, or TRANSIENT) and durable flag. |
Returns
ClassDecorator — A decorator applied to the provider class.
Examples
import { Injectable } from '@nestjs/common';
@Injectable()
export class CatsService {
private cats: string[] = [];
create(name: string) {
this.cats.push(name);
}
findAll() {
return this.cats;
}
}
import { Injectable, Scope } from '@nestjs/common';
@Injectable({ scope: Scope.REQUEST })
export class RequestScopedService {}
Notes
@Injectable() enables a class to be resolved via dependency injection.
Without it, constructor parameter metadata is not emitted and Nest cannot
instantiate the provider. The default scope is singleton; use REQUEST or
TRANSIENT scopes only when state must not be shared.