06 / 11

Walk through all four custom provider types: useClass, useValue, useFactory, useExisting.

useClass provides a class to be instantiated. useValue provides a static value or mock object. useFactory provides the return value of a factory function that supports async and dependency injection via the inject array. useExisting creates an alias so two tokens resolve to the same singleton instance.

All four custom provider forms
Key rules for each form:
  1. 1

    useClass — NestJS instantiates the class and manages its lifecycle.

  2. 2

    useValue — the value is used as-is; no instantiation or lifecycle management.

  3. 3

    useFactory — the inject array lists tokens resolved and passed as factory arguments in order.

  4. 4

    useFactory supports async; NestJS awaits the returned Promise before the app starts.

  5. 5

    useExisting — does NOT create a new instance; resolves the existing singleton for the referenced token.