The @Injectable() decorator marks a class so NestJS can create and manage its instances automatically. It helps with sharing and reusing code easily.
Injectable decorator in NestJS
@Injectable() export class ClassName { // class content }
The decorator goes just above the class definition.
It tells NestJS this class can be injected as a dependency.
@Injectable() export class CatsService { getCats() { return ['Tom', 'Jerry']; } }
@Injectable() export class DogsService { bark() { return 'Woof!'; } }
This example shows a simple GreetingService class marked with @Injectable(). It has a method to greet by name. The usage example creates an instance and prints a greeting.
import { Injectable } from '@nestjs/common'; @Injectable() export class GreetingService { sayHello(name: string): string { return `Hello, ${name}!`; } } // Usage example (not part of NestJS app): const service = new GreetingService(); console.log(service.sayHello('Friend'));
Always add @Injectable() to classes you want NestJS to manage.
Without it, NestJS cannot inject the class into other classes.
You can inject services into controllers or other services easily once they are injectable.
@Injectable() marks a class for NestJS dependency injection.
It helps create reusable and testable services.
Use it on any class you want NestJS to manage automatically.
Practice
@Injectable() decorator in NestJS?Solution
Step 1: Understand the role of
This decorator tells NestJS that the class can be injected as a dependency, making it reusable and manageable by the framework.@Injectable()Step 2: Differentiate from other decorators
Other decorators like@Controller()or database decorators serve different purposes, not dependency injection.Final Answer:
To mark a class so NestJS can manage it for dependency injection -> Option DQuick Check:
@Injectable()= marks class for DI [OK]
@Injectable() means NestJS manages the class [OK]- Confusing
@Injectable()with@Controller() - Thinking it creates routes or database connections
- Assuming it styles components
Solution
Step 1: Recall correct decorator syntax
In NestJS, decorators are functions called with parentheses, so@Injectable()is correct.Step 2: Check each option
class MyService {} lacks the decorator. function Injectable() { return class MyService {} } is invalid syntax. @Injectable class MyService {} misses parentheses after@Injectable.Final Answer:
@Injectable() class MyService {} -> Option CQuick Check:
Decorator syntax requires parentheses () [OK]
@Injectable() [OK]- Omitting parentheses after
@Injectable - Trying to use
@Injectablewithout decorator syntax - Confusing class and function syntax
appService.getMessage() is called?import { Injectable } from '@nestjs/common';
@Injectable()
class AppService {
getMessage() {
return 'Hello from AppService';
}
}
const appService = new AppService();
console.log(appService.getMessage());Solution
Step 1: Understand
The@Injectable()effect on class instantiation@Injectable()decorator marks the class for NestJS DI but does not change normal instantiation behavior.Step 2: Analyze the code execution
The code creates an instance manually and callsgetMessage(), which returns the string as expected.Final Answer:
Hello from AppService -> Option AQuick Check:
Manual instantiation still works; method returns string [OK]
- Assuming decorator breaks manual instantiation
- Expecting an error without NestJS container
- Thinking method returns undefined
import { Injectable } from '@nestjs/common';
@Injectable
export class UserService {
getUser() {
return 'User data';
}
}Solution
Step 1: Check decorator syntax
The@Injectabledecorator must be called with parentheses:@Injectable().Step 2: Verify other code parts
Class name casing is correct by convention. Method need not be async. Export keyword is present.Final Answer:
Missing parentheses after@Injectabledecorator -> Option AQuick Check:
Decorator requires () to work properly [OK]
@Injectable() [OK]- Forgetting parentheses after decorator
- Thinking method must be async
- Confusing export keyword presence
Solution
Step 1: Understand dependency injection benefits
Using@Injectable()allows NestJS to manage the logging service lifecycle and inject it automatically where needed.Step 2: Evaluate options for reusability and testability
Manual instantiation loses DI benefits. Plain objects lack lifecycle and DI.@Controller()is for routing, not services.Final Answer:
Use@Injectable()on the logging class and inject it where needed -> Option BQuick Check:
Injectable services = reusable and testable [OK]
@Injectable() for reusable NestJS services [OK]- Skipping
@Injectable()and instantiating manually - Using
@Controller()for services - Using plain objects without DI benefits
Ready to prove it?
Pass the test and this pattern moves unknown
