In my attempt to set up Nest with Fastify and Next using the fastify-next plugin, everything went smoothly until I added TypeOrm for MongoDB integration. Upon loading the AppModule, Nest throws an error indicating that the .next()
function cannot be found in the fastify
instance after the fastify-next
plugin is initialized.
Code
// main.ts
async function bootstrap() {
const app = await NestFactory.create<NestFastifyApplication>(
AppModule,
new FastifyAdapter(),
);
await app.init();
const configService = app.get<ConfigService<IConfigTypes>>(ConfigService);
const appConfig = configService.get<IAppConfig>('app');
const fastify = await app.register(fastifyNext, {
dev: appConfig.isDev,
dir: appConfig.clientPath,
});
fastify.after(() => {
appConfig.staticRoutes.forEach((page) => fastify.next(page));
});
await app.listen(appConfig.port);
Logger.log(`🚀 Server is running on port ${appConfig.port}`, 'Bootstrap');
}
bootstrap();
// app.module.ts
@Module({
imports: [
ConfigModule.forRoot({
validate,
isGlobal: true,
cache: true,
load: [appConfig, shopifyConfig, databaseConfig],
}),
TypeOrmModule.forRootAsync({
imports: [ConfigModule],
useFactory: (configService: ConfigService<IConfigTypes>) => {
const dbConfig = configService.get<IDatabaseConfig>('database');
const { type, host, base, user, pass } = dbConfig;
let url = 'mongodb+srv://';
url += `${user}:${pass}@${host}/${base}`;
url += '?retryWrites=true&w=majority';
return {
type,
url,
entities: [join(__dirname, '**/**.entity{.ts,.js}')],
synchronize: true,
useNewUrlParser: true,
logging: true,
};
},
inject: [ConfigService],
}),
],
})
export class AppModule {}
Console Output with Error:
[8:45:52 AM] File change detected. Starting incremental compilation...
[8:45:52 AM] Found 0 errors. Watching for file changes.
[Nest] 26331 - 08/23/2021, 8:45:53 AM LOG [NestFactory] Starting Nest application...
[Nest] 26331 - 08/23/2021, 8:45:53 AM LOG [InstanceLoader] AppModule dependencies initialized +26ms
[Nest] 26331 - 08/23/2021, 8:45:53 AM LOG [InstanceLoader] TypeOrmModule dependencies initialized +0ms
[Nest] 26331 - 08/23/2021, 8:45:53 AM LOG [InstanceLoader] ConfigHostModule dependencies initialized +1ms
[Nest] 26331 - 08/23/2021, 8:45:53 AM LOG [InstanceLoader] ConfigModule dependencies initialized +0ms
[Nest] 26331 - 08/23/2021, 8:45:53 AM LOG [InstanceLoader] ConfigModule dependencies initialized +0ms
[Nest] 26331 - 08/23/2021, 8:45:55 AM LOG [InstanceLoader] TypeOrmCoreModule dependencies initialized +2361ms
[Nest] 26331 - 08/23/2021, 8:45:55 AM LOG [NestApplication] Nest application successfully started +5ms
/dir/to/project/node_modules/avvio/boot.js:533
res = func()
^
TypeError: fastify.next is not a function
at /dir/to/project/src/server/main.ts:30:54 ===> appConfig.staticRoutes.forEach((page) => fastify.next(page));
To address this issue temporarily, I am currently waiting for the .next()
function to become available in the fastify
instance before executing the code. However, I believe there should be a better way to resolve this problem.
Modified Code:
async function bootstrap() {
const app = await NestFactory.create<NestFastifyApplication>(
AppModule,
new FastifyAdapter(),
);
await app.init();
const configService = app.get<ConfigService<IConfigTypes>>(ConfigService);
const appConfig = configService.get<IAppConfig>('app');
const fastify = await app.register(fastifyNext, {
dev: appConfig.isDev,
dir: appConfig.clientPath,
});
await new Promise((res) => {
const launch = async () => {
if (!fastify.next) {
setTimeout(launch, 200);
return;
}
appConfig.staticRoutes.forEach((page) => fastify.next(page));
await app.listen(appConfig.port);
Logger.log(`🚀 Server is running on port ${appConfig.port}`, 'Bootstrap');
res(null);
};
launch();
});
}
bootstrap();
The error disappears when I remove the TypeOrmModule
from AppModule
, indicating that the issue may stem from the asynchronous loading of the Module forRootAsync
.
If you have any insights or better solutions to rectify this problem, please feel free to share. Thank you!