Assuming you have the basic NestJS setup:
async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.enableCors(corsOptions);
app.useGlobalPipes(
new ValidationPipe({
validateCustomDecorators: true,
transform: true,
whitelist: true,
exceptionFactory: exceptionFactory,
}),
);
app.useGlobalFilters(new CustomExceptionsFilter());
const port = parseInt(process.env.PORT);
await app.listen(port);
}
Can I access the app
variable created with NestFactory from a service in a different module?
@Injectable()
export class TestService {
constructor(
...
) {}
async useApp(): Promise<any> {
const app = ... // use Injected app instance created from AppModule
}
I've tried importing AppModule and creating a NestFactory again, but it just resolves into redundancies.
NestFactory.create
will create theapp
, which depends on modules & their providers. Usingapp
before it has been created does not make sense.Instead, you could tell us what you're trying to do with that
app
atTestService
.