08 / 12

How does NestJS resolve a provider when the same token is exported by two different imported modules?

The last provider registered for a given token wins. NestJS uses the order of the imports array to determine precedence — the module listed later takes priority. This silent shadowing is a common source of bugs and is best prevented by using unique namespaced tokens such as Symbols or InjectionToken.

Token collision — last import wins
How to prevent token collision:
  1. 1

    Use Symbol() or InjectionToken for all non-class provider tokens — they are inherently unique.

  2. 2

    Namespace string tokens with the module name: 'AuthModule.JwtSecret' not just 'JwtSecret'.

  3. 3

    Avoid having two modules export providers under the same token.

  4. 4

    Document which module is the authoritative source for shared infrastructure tokens.

  5. 5

    Token shadowing is silent — there are no warnings at startup, only wrong behavior at runtime.