10 / 11

What is the difference between useExisting and useClass when aliasing a provider in NestJS?

Difficulty: 5/10
provider aliasing, dependency injection, NestJS modules

useClass with a class always creates a new independent instance managed by the container. useExisting resolves and returns the same singleton instance of the referenced token — no new instantiation occurs. Use useExisting when you need backward-compatible aliases without duplicating state.

useClass vs useExisting — instance behavior
When to use each:
  1. 1

    useClass — when you want an independent instance with its own state.

  2. 2

    useExisting — when you want two token names to point to the exact same runtime object.

  3. 3

    useExisting is useful for maintaining backward-compatible tokens in evolving APIs.

  4. 4

    useExisting requires the referenced token to already be registered in the same module or an imported module.

Scenario Questions

0-2 years experience

  1. 1You have a service A that depends on an interface IMyService. How would you configure the module to provide a concrete class MyService using useClass, and what would you change to make it use an existing provider instead?
  2. 2If you alias a provider with useExisting, what happens at runtime when another class injects the token? Explain the flow compared to useClass.
  3. 3Suppose you accidentally used useExisting when you intended to create a new instance. What would you observe in the behavior of the injected service?

2-5 years experience

  1. 1During a feature rollout, you switched a provider from useClass to useExisting to share a singleton across two modules, but the feature started throwing 'undefined method' errors. Walk me through how you would debug this issue.
  2. 2You need to replace a heavy third‑party implementation with a lightweight mock for a specific module without affecting other modules. Would you choose useClass or useExisting for the alias, and why?
  3. 3Explain a scenario where using useExisting could cause a circular dependency, and how you would resolve it.

5-8 years experience

  1. 1In a large microservice architecture, several modules import a shared AuthModule that provides an AuthService. Discuss the trade‑offs of aliasing AuthService with useClass versus useExisting when you need to extend its behavior in some modules.
  2. 2Your team wants to introduce a dynamic module that conditionally provides different implementations of a Logger based on environment variables. How would you design the provider registration using useClass and useExisting to avoid multiple instances and keep startup time low?
  3. 3Consider performance implications of useExisting vs useClass when the aliased provider is a request‑scoped service. What impact does each have on memory and request handling?

8+ years experience

  1. 1A legacy monorepo contains dozens of NestJS modules that alias core services using useClass. Management wants to refactor to useExisting to enforce a single source of truth and reduce bundle size. Outline a migration plan, including risks, testing strategy, and how you’d ensure backward compatibility.
  2. 2When designing a cross‑team shared library of utilities, you need to decide whether to expose providers via useClass or useExisting to allow teams to override implementations without breaking existing contracts. What architectural guidelines would you set, and how would you enforce them?
  3. 3If you anticipate future hot‑module replacement (HMR) for certain services, how does the choice between useClass and useExisting affect the ability to swap implementations at runtime across the whole application?

Follow-up Questions

  • Can you give an example where useExisting is clearly better than useClass?
  • How does NestJS manage the lifecycle of a provider that is aliased with useExisting?
  • What testing considerations arise when you alias a provider with useExisting?
Share

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.