What is the @Global() decorator in NestJS?
Answer
The @Global() decorator makes a module globally available — its exported providers can be used anywhere without importing the module. Apply it to the module class: @Global() @Module({ providers: [ConfigService], exports: [ConfigService] }) class ConfigModule {}. Once ConfigModule is imported once (usually in AppModule), ConfigService is available for injection in any other module without re-importing. Use sparingly — global modules make dependencies implicit and can make code harder to understand. Appropriate use cases: configuration, logging, database connections, and utilities that are needed universally. Prefer explicit imports for most modules to make dependencies clear and testable.
Previous
How does NestJS handle circular dependencies?
Next
How do you implement file uploads in NestJS?
More NestJS Questions
View all →- Intermediate How does NestJS dependency injection scoping work?
- Intermediate What are NestJS microservices and what transports are supported?
- Intermediate What is the ExecutionContext in NestJS?
- Intermediate How do you implement role-based access control (RBAC) in NestJS?
- Intermediate What is the Reflector in NestJS and how is it used?