When several parts of an application need the same business logic, copying that logic into every controller quickly becomes difficult to maintain. NestJS solves this with providers: classes or values that the framework can create and make available through dependency injection.
What is a provider?
A service is the most common kind of provider. It usually contains business logic that controllers or other services need.
import { Injectable } from '@nestjs/common';
@Injectable()
export class ProductsService {
findAll() {
return ['Keyboard', 'Monitor'];
}
}
The @Injectable() decorator marks the class as available for Nestโs dependency injection system. It does not register the class by itself; a module must include it.
Register the provider
@Module({
controllers: [ProductsController],
providers: [ProductsService],
})
export class ProductsModule {}
Now a controller in that module can receive the service through its constructor:
@Controller('products')
export class ProductsController {
constructor(private readonly products: ProductsService) {}
@Get()
findAll() {
return this.products.findAll();
}
}
Nest resolves the dependency and supplies the provider instance. By default, providers are shared as singleton instances across the application context rather than recreated for every injection.
Providers are not only services
A provider can represent a repository, helper, configuration value, or an adapter around an external library. The important part is that Nest knows its token and how to resolve it.
Providers are also scoped to modules. If another module needs one, export it from the declaring module and import that module where it is required. This makes sharing explicit instead of exposing every class globally.
When should you create a service?
Create a provider when logic needs to be reused, tested independently, or kept separate from HTTP request handling. Keep controllers focused on routing and request/response concerns; put business operations in services.
The takeaway: a provider is something Nest can manage and inject. Modules register providers, and dependency injection connects them to the classes that need them.
Reference: NestJS Providers