Kind: Function
Source: packages/core/hooks/on-module-init.hook.ts
Part of: Core
Calls the onModuleInit function on the module and its children
(providers / controllers).
callModuleInitHook runs the onModuleInit() lifecycle hook for a Nest module’s initialized controllers, providers, injectables, and middleware. It processes non-transient and transient instances before invoking the module class’s own hook when its dependency tree is static.
Signature
tsasync function callModuleInitHook(module: Module): Promise<void>
Parameters
| Name | Type |
|---|---|
module | Module |
Returns: Promise<void>
Diagram
mermaidgraph LR A[Module] --> B[Controllers] A --> C[Providers] A --> D[Injectables] A --> E[Middleware] B --> F[Invoke onModuleInit] C --> F D --> F E --> F F --> G[Non-transient instances] G --> H[Transient instances] H --> I[Module class onModuleInit]
Usage
tsimport { callModuleInitHook } from '@nestjs/core/hooks/on-module-init.hook';
import { Module } from '@nestjs/core/injector/module';
// Typically called internally by Nest during application initialization.
async function initializeModule(moduleRef: Module) {
await callModuleInitHook(moduleRef);
console.log('Module lifecycle hooks have completed.');
}
AI Coding Instructions
- Treat this as an internal lifecycle utility; application code should usually implement
OnModuleInitrather than call this function directly. - Ensure providers, controllers, injectables, and middleware are fully instantiated before invoking module initialization hooks.
- Preserve the ordering: execute child instance hooks before the module class’s own
onModuleInit()hook. - Keep transient and non-transient instance handling separate so each lifecycle hook is invoked for the correct resolved instances.
- Only invoke the module class hook when its dependency tree is static and the class implements
onModuleInit().
Relationships
- IMPORTS →
OnModuleInit - IMPORTS →
isFunction - IMPORTS →
isNil
Was this page helpful?