Kind: Constant
Source: packages/core/nest-factory.ts
Part of: Core
Use NestFactory to create an application instance.
Specifying an entry module
Pass the required root module for the application via the module parameter.
By convention, it is usually called ApplicationModule. Starting with this
module, Nest assembles the dependency graph and begins the process of
Dependency Injection and instantiates the classes needed to launch your
application.
NestFactory creates and initializes Nest application instances from a root module. It starts the dependency-injection graph, configures the selected runtime adapter, and returns an application object that can be configured and started.
Definition
tsnew NestFactoryStatic()
Value
tsnew NestFactoryStatic()
Diagram
mermaidgraph LR A[ApplicationModule] --> B[NestFactory] B --> C[Dependency Injection Container] C --> D[Providers and Controllers] B --> E[Nest Application Instance] E --> F[Configure Middleware / Pipes / Guards] F --> G[app.listen()]
Usage
tsimport { NestFactory } from '@nestjs/core';
import { ApplicationModule } from './application.module';
async function bootstrap() {
const app = await NestFactory.create(ApplicationModule);
app.setGlobalPrefix('api');
await app.listen(3000);
}
bootstrap();
AI Coding Instructions
- Pass the application's root module (commonly
ApplicationModuleorAppModule) as the first argument toNestFactory.create(). - Await application creation before configuring global middleware, pipes, guards, interceptors, or filters.
- Configure the returned application instance before calling
app.listen()so startup behavior is consistent. - Use the appropriate factory method for the runtime:
create()for HTTP applications,createMicroservice()for microservices, andcreateApplicationContext()for standalone contexts.
Was this page helpful?