Module Internals

Preview — 3 of 10 questions

During bootstrap, what does Nest's DependenciesScanner/compiler do before any provider is instantiated?

javascript
// Conceptual: scan populates the container before instantiation
// container.addModule(metatype, scope)
// container.addProvider(provider, token)  → creates InstanceWrapper
// then InstanceLoader.createInstancesOfDependencies()
AIt executes every controller method once to warm caches
BIt recursively scans modules and their metadata to build a graph of modules, providers, controllers, and their relationships, registering them in the NestContainer
CIt compiles TypeScript to JavaScript on the fly
DIt opens database connections eagerly

What is the key difference between moduleRef.get() and moduleRef.resolve()?

javascript
@Injectable()
export class Service {
  constructor(private moduleRef: ModuleRef) {}

  async work(): Promise<void> {
    const singleton = this.moduleRef.get(ConfigService); // sync, shared
    const scoped = await this.moduleRef.resolve(RequestScopedSvc); // async, fresh
  }
}
Aget() is async; resolve() is sync
BThey are identical aliases
Cget() retrieves a singleton (DEFAULT-scoped) instance synchronously; resolve() is async and returns a fresh instance for scoped (REQUEST/TRANSIENT) providers, optionally tied to a ContextId
Dresolve() only works for controllers

What role does the MODULE_PATH metadata serve in Nest?

javascript
import { RouterModule } from '@nestjs/core';

@Module({
  imports: [
    AdminModule,
    RouterModule.register([{ path: 'admin', module: AdminModule }]),
  ],
})
export class AppModule {}
AIt can prefix routes for all controllers within a module (module-level route path), and is part of how Nest associates modules during scanning
BIt sets the file system path for hot reload
CIt defines the npm package name
DIt is the database schema name

Sign up free to play

Answer all 10 questions (7 more), see explanations for every answer, and track your score.