CodePark/ questions / Modules & architecture
START PRACTICINGPRACTICE
Backend question · NestJS › Modules & architecture

What does @Global() do on a module, and why should you use it sparingly?

@Global() makes a module's exported providers available in every module. Other modules don't need to list it in imports.

You still register the global module once, usually in the root module. Good candidates are things almost every module needs:

  • configuration
  • a logger
  • a database connection

Many official modules offer this as an option. For example, ConfigModule.forRoot() accepts isGlobal: true.

Why use it sparingly? The docs advise against making everything global:

  • Explicit imports show what each module depends on, and they keep the dependency graph easy to read.
  • Global modules hide those relationships, so it's harder to see what a feature really depends on.
  • Global modules make modules less portable, so they are harder to move or reuse.
  • Exported providers can be injected everywhere without importing the module.
  • You register the global module once, usually in the root module.
  • It is good for config, logging and the database connection.
  • Overusing it hides dependencies and hurts the design.