0
0
NestJSframework~10 mins

Injectable decorator in NestJS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Injectable decorator
Define class
Add @Injectable decorator
NestJS registers class as provider
Class can be injected into constructors
NestJS creates instance when needed
Use instance in dependent classes
Shows how adding @Injectable marks a class for NestJS to manage and inject where needed.
Execution Sample
NestJS
import { Injectable } from '@nestjs/common';

@Injectable()
export class CatsService {
  getCats() { return ['Tom', 'Jerry']; }
}
Defines a service class marked with @Injectable so NestJS can inject it.
Execution Table
StepActionEvaluationResult
1Read class CatsServiceClass definedClass CatsService created
2Apply @Injectable decoratorDecorator marks classClass registered as provider
3NestJS scans providersFinds CatsServiceCatsService added to DI container
4Another class requests CatsServiceConstructor injectionNestJS creates CatsService instance
5Call getCats()Method runsReturns ['Tom', 'Jerry']
6End of flowNo more actionsExecution stops
💡 All steps complete; class is injectable and usable via NestJS DI
Variable Tracker
VariableStartAfter Step 2After Step 4Final
CatsServiceundefinedRegistered as providerInstance createdInstance used
Key Moments - 2 Insights
Why do we need the @Injectable decorator on a class?
Without @Injectable, NestJS does not register the class as a provider, so it cannot inject it. See execution_table step 2 where the decorator marks the class.
What happens when another class requests CatsService in its constructor?
NestJS looks up CatsService in its container and creates an instance to inject. See execution_table step 4 for this injection process.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what happens at step 3?
AAnother class requests CatsService
BCatsService method getCats() is called
CNestJS registers CatsService as a provider
DThe class CatsService is defined
💡 Hint
Check the 'Action' and 'Result' columns at step 3 in the execution_table
At which step does NestJS create an instance of CatsService?
AStep 4
BStep 5
CStep 2
DStep 1
💡 Hint
Look for 'Instance created' in the 'Result' column in execution_table
If we remove @Injectable decorator, which step would fail?
AStep 1
BStep 3
CStep 2
DStep 5
💡 Hint
Without @Injectable, NestJS cannot register the class as provider (step 3)
Concept Snapshot
@Injectable decorator marks a class as a provider
NestJS registers it in the dependency injection container
Allows class instances to be injected into constructors
Enables reuse and easy testing
Without it, injection fails
Use on services, repositories, and providers
Full Transcript
The @Injectable decorator in NestJS marks a class so the framework knows to manage it as a provider. When NestJS starts, it scans for classes with @Injectable and registers them in its dependency injection container. Later, when another class needs this provider, NestJS creates an instance and injects it automatically. This process allows easy sharing and reuse of services. Without @Injectable, NestJS will not recognize the class for injection, causing errors. The execution flow starts with defining the class, applying the decorator, registering the provider, injecting the instance, and finally using the class methods.