What is the purpose of the @Injectable() decorator in NestJS?
The <code>@Injectable()</code> decorator marks a class as a provider that can be managed by NestJS's dependency injection system. It allows the class to be injected into other classes as a dependency.
Click to reveal answer
beginner
Can a class without <code>@Injectable()</code> be injected as a provider in NestJS?
No. Without the <code>@Injectable()</code> decorator, NestJS does not recognize the class as a provider and will not inject it into other classes.
Click to reveal answer
beginner
How does @Injectable() help with managing dependencies in NestJS?
It tells NestJS to create and manage an instance of the class automatically. This way, you don't have to manually create instances or manage dependencies yourself.
Click to reveal answer
intermediate
Is it possible to add metadata or options inside the @Injectable() decorator?
Yes. You can pass options to @Injectable() to control the provider's scope, like making it singleton or request-scoped.
Click to reveal answer
beginner
What happens if you forget to add @Injectable() on a service you want to inject?
NestJS will throw a runtime error saying it cannot resolve the dependency because the class is not registered as a provider.
Click to reveal answer
What does the @Injectable() decorator do in NestJS?
ARegisters a middleware
BDefines a controller route
CCreates a database connection
DMarks a class as a provider for dependency injection
✗ Incorrect
@Injectable() marks a class as a provider so NestJS can inject it where needed.
Can you inject a class without @Injectable() in NestJS?
AYes, always
BOnly if it is a controller
CNo, it must have <code>@Injectable()</code>
DOnly if it extends another class
✗ Incorrect
Classes must have @Injectable() to be injected as providers.
Which of these is a valid use of @Injectable() options?
ASetting the provider scope to singleton
BDefining HTTP routes
CConnecting to a database
DStyling components
✗ Incorrect
You can set provider scope like singleton or request-scoped using options in @Injectable().
What error occurs if you inject a class without @Injectable()?
ACannot resolve dependency error
BSyntax error
CType error
DNo error
✗ Incorrect
NestJS throws a 'Cannot resolve dependency' error at runtime.
Where should you place the @Injectable() decorator?
AOn HTML templates
BOn classes you want to inject as providers
COn environment configuration files
DOn database schemas
✗ Incorrect
@Injectable() goes on classes to make them injectable providers.
Explain in your own words what the @Injectable() decorator does in NestJS and why it is important.
Think about how NestJS knows which classes to create and share automatically.
You got /4 concepts.
Describe what happens if you try to inject a class without the @Injectable() decorator in NestJS.
Consider what NestJS needs to know to inject a class.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of the @Injectable() decorator in NestJS?
easy
A. To style components with CSS
B. To define a route handler in a controller
C. To create a database connection automatically
D. To mark a class so NestJS can manage it for dependency injection
Solution
Step 1: Understand the role of @Injectable()
This decorator tells NestJS that the class can be injected as a dependency, making it reusable and manageable by the framework.
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 D
Quick Check:
@Injectable() = marks class for DI [OK]
Hint: Remember: @Injectable() means NestJS manages the class [OK]
Common Mistakes:
Confusing @Injectable() with @Controller()
Thinking it creates routes or database connections
Assuming it styles components
2. Which of the following is the correct syntax to make a service class injectable in NestJS?
easy
A. function Injectable() { return class MyService {} }
B. class MyService {}
C. @Injectable() class MyService {}
D. @Injectable class MyService {}
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 C
Quick Check:
Decorator syntax requires parentheses () [OK]
Hint: Always use parentheses with decorators like @Injectable() [OK]
Common Mistakes:
Omitting parentheses after @Injectable
Trying to use @Injectable without decorator syntax
Confusing class and function syntax
3. Given the following code, what will be the output when 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());
medium
A. Hello from AppService
B. undefined
C. Error: AppService is not injectable
D. null
Solution
Step 1: Understand @Injectable() effect on class instantiation
The @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 calls getMessage(), which returns the string as expected.
Final Answer:
Hello from AppService -> Option A
Quick Check:
Manual instantiation still works; method returns string [OK]
Hint: Manual new works; decorator doesn't block normal class use [OK]
Common Mistakes:
Assuming decorator breaks manual instantiation
Expecting an error without NestJS container
Thinking method returns undefined
4. Identify the error in this NestJS service code:
import { Injectable } from '@nestjs/common';
@Injectable
export class UserService {
getUser() {
return 'User data';
}
}
medium
A. Missing parentheses after @Injectable decorator
B. Class name should be lowercase
C. Method getUser must be async
D. Missing export keyword
Solution
Step 1: Check decorator syntax
The @Injectable decorator 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 @Injectable decorator -> Option A
Quick Check:
Decorator requires () to work properly [OK]
Hint: Always add () after decorators like @Injectable() [OK]
Common Mistakes:
Forgetting parentheses after decorator
Thinking method must be async
Confusing export keyword presence
5. You want to create a reusable logging service in NestJS that other services can inject. Which of the following is the best way to define it?
hard
A. Create a class without @Injectable() and instantiate it manually everywhere
B. Use @Injectable() on the logging class and inject it where needed
C. Define logging functions in a plain JavaScript object and export it
D. Use @Controller() decorator on the logging class
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 B
Quick Check:
Injectable services = reusable and testable [OK]
Hint: Always use @Injectable() for reusable NestJS services [OK]
Common Mistakes:
Skipping @Injectable() and instantiating manually
Using @Controller() for services
Using plain objects without DI benefits
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.