0
0
NestJSframework~10 mins

Custom providers in NestJS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Custom providers
Define provider token
Create provider object with useClass/useValue/useFactory
Register provider in module's providers array
Inject provider using token in constructor
Use provider instance in service/component
This flow shows how to create and use a custom provider in NestJS by defining a token, registering it, and injecting it where needed.
Execution Sample
NestJS
const MY_TOKEN = 'MY_TOKEN';

const myProvider = {
  provide: MY_TOKEN,
  useValue: { greet: () => 'Hello!' }
};
This code defines a custom provider with a token and a simple object value to be injected.
Execution Table
StepActionEvaluationResult
1Define token MY_TOKENMY_TOKEN is a string constantToken ready for provider
2Create provider object with provide and useValueuseValue is an object with greet methodProvider object created
3Register provider in module's providers arrayModule now knows MY_TOKEN providerProvider registered
4Inject provider in constructor using @Inject(MY_TOKEN)Injection token matches providerProvider instance available
5Call greet() method on injected providergreet() returns 'Hello!'Output: 'Hello!'
💡 Execution stops after provider is injected and used successfully
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
MY_TOKENundefined'MY_TOKEN''MY_TOKEN''MY_TOKEN''MY_TOKEN'
myProviderundefined{ provide: 'MY_TOKEN', useValue: { greet: [Function] } }Registered in moduleInjected instance availableInstance with greet method
InjectedInstanceundefinedundefinedundefined{ greet: [Function] }Used to call greet()
Key Moments - 3 Insights
Why do we need a token like MY_TOKEN instead of just using the class name?
The token uniquely identifies the provider, especially when using useValue or useFactory, so NestJS knows which provider to inject. See execution_table step 1 and 4.
What happens if the token in @Inject() does not match any registered provider?
NestJS throws an error because it cannot find a provider for that token. This is why matching tokens in steps 3 and 4 is crucial.
Can we use useClass instead of useValue in the provider object?
Yes, useClass lets NestJS instantiate a class for the provider. useValue provides a fixed object. Both are valid ways to create custom providers.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the result after step 5?
AOutput: 'Hello!'
BError because token is missing
CProvider instance is available but greet() is not called
DProvider object is undefined
💡 Hint
Check execution_table row with Step 5 for the output result
At which step is the provider registered in the module?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look for the step mentioning 'Register provider in module's providers array'
If we change useValue to useClass in the provider, what changes in variable_tracker?
AInjectedInstance becomes undefined
BMY_TOKEN value changes
CmyProvider becomes a class reference instead of an object
DNo changes at all
💡 Hint
Check variable_tracker row for myProvider and think about useClass vs useValue
Concept Snapshot
Custom providers in NestJS:
- Define a unique token (string or symbol)
- Create provider object with provide and useClass/useValue/useFactory
- Register provider in module's providers array
- Inject provider using @Inject(token) in constructor
- Use injected instance in your service/component
Full Transcript
Custom providers in NestJS let you define how dependencies are created and injected. First, you define a unique token to identify the provider. Then, you create a provider object specifying the token and how to create the value, using useClass for classes, useValue for fixed objects, or useFactory for functions. You register this provider in the module's providers array. When you want to use it, you inject it in a constructor using the @Inject decorator with the same token. This process allows you to customize dependency injection beyond just classes. The execution steps show defining the token, creating the provider, registering it, injecting it, and finally using the injected instance. Key points include matching tokens exactly and choosing the right provider type. This method helps you control what gets injected and how, making your code flexible and testable.