Skip to content

InjectableOptions

reference
1 min readUpdated

Kind: Type

Source: packages/common/decorators/core/injectable.decorator.ts

Part of: Common

Defines the injection scope.

InjectableOptions configures how a provider is registered when using NestJS's @Injectable() decorator. Its primary responsibility is defining the provider's injection scope, which controls whether Nest creates a shared singleton, a per-request instance, or a transient instance.

Definition

ts
ScopeOptions

Diagram

mermaid
graph LR
  A["@Injectable(options)"] --> B["InjectableOptions"]
  B --> C["scope?: Scope"]
  C --> D["DEFAULT<br/>Shared singleton"]
  C --> E["REQUEST<br/>One instance per request"]
  C --> F["TRANSIENT<br/>New instance per injection"]
  A --> G["Nest DI Container"]

Usage

ts
import { Injectable, Scope } from '@nestjs/common';

@Injectable({
  scope: Scope.REQUEST,
})
export class RequestContextService {
  private readonly createdAt = new Date();

  getCreatedAt(): Date {
    return this.createdAt;
  }
}

AI Coding Instructions

  • Use InjectableOptions as the options object passed to @Injectable().
  • Prefer the default scope for stateless services and shared infrastructure such as repositories or API clients.
  • Use Scope.REQUEST only when a provider needs request-specific state, such as tenant or correlation context.
  • Use Scope.TRANSIENT for providers that must be recreated for every injection site.
  • Be aware that request-scoped or transient dependencies can cause otherwise singleton consumers to become scoped as well.

Was this page helpful?

Download as PDF
InjectableOptions — NestJS head-to-head