Hello!
I tried to use ServerStaticModule in my Nest.js app but it just doesn't want to work.
I've got error:
[Nest] 2724 - 03.05.2023, 03:10:41 ERROR [ExceptionHandler] Nest can't resolve dependencies of
the ServeStaticModule (SERVE_STATIC_MODULE_OPTIONS, AbstractLoader, ?). Please make sure that the
argument HttpAdapterHost at index [2] is available in the ServeStaticModule context.
Potential solutions:
- Is ServeStaticModule a valid NestJS module?
- If HttpAdapterHost is a provider, is it part of the current ServeStaticModule?
- If HttpAdapterHost is exported from a separate
@Module, is that module imported within ServeStaticModule?
@Module({
imports: [ /* the Module containing HttpAdapterHost */ ]
})
Here's my code for app.module.ts:
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { DiscordModule } from './discord/discord.module';
import { LoggerModule } from './logger/logger.module';
import { ConfigModule } from '@nestjs/config';
import { AuthModule } from './auth/auth.module';
import { UserModule } from './user/user.module';
import { SessionSerializer } from './auth/session.serializer';
import { DiscordStrategy } from './auth/discord.strategy';
import { ChannelModule } from './channel/channel.module';
import { DiscordService } from './discord/discord.service';
import { TypeORMSession } from './database/entities/session.entity';
import { DatabaseModule } from './database/database.module';
import { ServeStaticModule } from '@nestjs/serve-static';
import { join } from 'path';
let envFilePath = '.env.development';
let ignoreEnvFile = false;
if (process.env.NODE_ENV === 'production') {
// Check if .env.production exists
const fs = require('fs');
if (fs.existsSync('.env.production')) {
console.log('Using .env.production file');
envFilePath = '.env.production';
}
else {
console.log('No .env.production file found, using injected environment variables');
ignoreEnvFile = true;
}
} else {
console.log('Using .env.development file');
}
@Module({
imports: [
ConfigModule.forRoot({
isGlobal: true,
envFilePath,
ignoreEnvFile,
}),
ServeStaticModule.forRoot({
rootPath: join(__dirname, '../../frontend/dist'), // <-- path to the static files
exclude: ['/api*'],
}),
DatabaseModule,
DiscordModule,
LoggerModule,
AuthModule,
UserModule,
ChannelModule,
],
controllers: [
AppController,
],
providers: [
AppService,
SessionSerializer,
DiscordStrategy,
DiscordService,
TypeORMSession,
],
exports: [
SessionSerializer,
AppService,
DiscordService,
TypeORMSession,
],
})
export class AppModule {}
Any idea on what should I do?
