For AI agents: the complete documentation index is available at https://navios.tech/llms.txt, the full documentation bundle is available at https://navios.tech/llms-full.txt, and this page is available as Markdown at https://navios.tech/docs/di/essentials/scopes.md.

Scopes

A scope says how long an instance lives and who shares it. Every provider has one; the default is singleton.

ScopeInstancesStored inDestroyed when
InjectableScope.SingletonOne per root containerThe root containerThe container is disposed, or it is invalidated
InjectableScope.TransientA new one for every inject or getNowhereIts owner is destroyed
InjectableScope.RequestOne per ScopedContainerThe scope that created itThe scope is disposed
import { Injectable, InjectableScope } from '@navios/di'

@Injectable({ scope: InjectableScope.Request })
class RequestState {
  readonly values = new Map<string, unknown>()
}

Singleton

One instance serves the whole container. DatabaseService and UserService from the quick start are singletons, which fits anything stateless or deliberately shared: clients, pools, repositories, configuration.

A singleton is always built against the root container, even when it is first requested through a scope. It can therefore never see a value that belongs to one scope.

Concurrent get() calls for a singleton that is still initialising all receive the same instance; it is built once.

Transient

Each resolution builds a new instance. Two inject() calls in one class give two instances:

import { inject, Injectable, InjectableScope } from '@navios/di'

@Injectable({ scope: InjectableScope.Transient })
class Counter {
  value = 0
}

@Injectable()
class Stats {
  readonly hits = inject(Counter)
  readonly misses = inject(Counter) // a different Counter
}

A transient injected into a singleton is built once, together with the singleton, and lives as long as it does. A method that needs a new one on every call injects the Container and resolves the transient there.

A transient is not stored, so it is owned by whatever created it:

Created byDestroyed
inject() inside a serviceTogether with that service
get() or getSync() on a scopeWhen the scope is disposed
get() or getSync() on the rootNever by the container. Its onServiceDestroy is not called

Request

A request-scoped service exists once per ScopedContainer. The name comes from servers, where a scope lasts for one HTTP request, but a scope can wrap any unit of work: a job, a message, a CLI command.

Creating a scope

container.createScope() returns a ScopedContainer. It resolves like a container and must be disposed when the work is done.

import { Container } from '@navios/di'

const container = new Container()

const scope = container.createScope()
try {
  const state = await scope.get(RequestState)
  const same = await scope.get(RequestState) // the same instance
  const users = await scope.get(UserService) // the container's singleton
} finally {
  await scope.dispose()
}

With TypeScript 5.2 or newer, await using disposes the scope at the end of the block:

{
  await using scope = container.createScope()
  const state = await scope.get(RequestState)
}

Scopes are independent of each other. Any number can be open at once, each with its own RequestState, all sharing the root's singletons. Creating one is cheap; request-scoped services are built only when something asks for them.

A request-scoped service is found through the scope object it is resolved from. There is no ambient context and no AsyncLocalStorage, so pass the scope to the code that resolves from it.

After dispose(), get() rejects and getSync() and addInstance() throw a DIError with code ContainerDisposed. A second dispose() returns the first one. A scope that is never disposed stays linked to the singletons its services injected, which costs each of them somewhat more than a kilobyte per scope for as long as they live.

The current scope

inject(ScopedContainer) gives a request-scoped service, or a transient inside one, the scope it belongs to. Use it to resolve request-scoped services later, when inject(Container) would give the root:

import { inject, Injectable, InjectableScope, ScopedContainer } from '@navios/di'

@Injectable({ scope: InjectableScope.Request })
class Checkout {
  private readonly scope = inject(ScopedContainer)

  async pay() {
    const payment = await this.scope.get(PaymentSession)
    return payment.run()
  }
}

Values supplied per scope

Data that exists only for one unit of work enters the scope through addInstance(). Declare a token with a scope for it:

import { inject, Injectable, InjectableScope, InjectionToken } from '@navios/di'

export const REQUEST_ID = InjectionToken.create<string>('RequestId', {
  scope: InjectableScope.Request,
})

@Injectable({ scope: InjectableScope.Request })
class RequestLogger {
  private readonly requestId = inject(REQUEST_ID)

  log(message: string) {
    console.log(`[${this.requestId}] ${message}`)
  }
}

await using scope = container.createScope()
scope.addInstance(REQUEST_ID, crypto.randomUUID())

const logger = await scope.get(RequestLogger)

createScope() also accepts a plain metadata object, read back with scope.getMetadata(key) and changed with scope.setMetadata(key, value). Metadata is not injectable; it is for the code that owns the scope.

Scope rules

A service may only depend on things that live at least as long as it does.

ConsumerInjects a singletonInjects a request-scoped serviceInjects a transient
SingletonYesNoYes, if the transient needs nothing scoped
RequestYesYesYes
TransientYesOnly when its own consumer is allowed toYes

The container enforces the table. It never changes a service's scope to make a graph work.

ScopeMismatchError

Breaking the rule fails with a DIError with code ScopeMismatchError. The message names the path that led there:

@Injectable()
class AuditService {
  private readonly state = inject(RequestState)
}

await scope.get(AuditService)
// Scope mismatch: RequestState is request-scoped and cannot be resolved from the root
// container (AuditService -> RequestState). Resolve it from a scope created with
// container.createScope(), and do not inject it into a singleton.

The error is the same whether AuditService is requested from the root or from a scope, and whether it uses inject() or optional(). A transient in between does not hide it. Resolving a request-scoped service directly from the root container is the same error without a path.

There are three ways out:

FixWhen
Make the consumer request-scopedThe service needs per-request state to do its work, so it is per-request itself
Pass the value as an argumentThe service should stay a singleton. Hand the request-scoped object to the method that needs it
Split the serviceOnly part of it needs request state. Move that part into a request-scoped service that injects the rest