#HttpAdapterHost not available in ServerStaticModule dependencies

35 messages · Page 1 of 1 (latest)

umbral hare
#

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?

#

Any info on how to handle those kind of errors in future and what they mean would be greatly appreciated

swift vector
umbral hare
#

should I remove yarn.lock?

#
at Injector.lookupComponentInParentModules (G:\Dokumenty\Projekty\pla-manager\backend\node_modules\@nestjs\core\injector\injector.js:247:19)   
    at processTicksAndRejections (node:internal/process/task_queues:95:5)
    at Injector.resolveComponentInstance (G:\Dokumenty\Projekty\pla-manager\backend\node_modules\@nestjs\core\injector\injector.js:200:33)
    at resolveParam (G:\Dokumenty\Projekty\pla-manager\backend\node_modules\@nestjs\core\injector\injector.js:120:38)
    at async Promise.all (index 2)
    at Injector.resolveConstructorParams (G:\Dokumenty\Projekty\pla-manager\backend\node_modules\@nestjs\core\injector\injector.js:135:27)
    at Injector.loadInstance (G:\Dokumenty\Projekty\pla-manager\backend\node_modules\@nestjs\core\injector\injector.js:61:13)
    at Injector.loadProvider (G:\Dokumenty\Projekty\pla-manager\backend\node_modules\@nestjs\core\injector\injector.js:88:9)
    at G:\Dokumenty\Projekty\pla-manager\backend\node_modules\@nestjs\core\injector\instance-loader.js:56:13
    at async Promise.all (index 0)```
swift vector
#

what's the output of nest info ?

umbral hare
# swift vector what's the output of `nest info` ?
[System Information]
OS Version     : Windows 10
NodeJS Version : v19.1.0
YARN Version    : 1.22.19 

[Nest CLI]
Nest CLI Version : 9.4.2

[Nest Platform Information]
platform-express version : 9.4.0
schematics version       : 9.1.0
passport version         : 9.0.3
typeorm version          : 9.0.1
common version           : 9.4.0
config version           : 2.3.1
core version             : 9.4.0
cli version              : 9.4.2```
swift vector
#

hm, that's weird

umbral hare
#

Not very helpful sadly

#

HttpAdapterHost not available in ServerStaticModule dependencies

swift vector
umbral hare
#

I removed them, nothing changed

swift vector
#

can you show us the main.ts entry file?

umbral hare
# swift vector can you show us the `main.ts` entry file?

Of course, here it is:

import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { TypeORMSession } from './database/entities/session.entity';
import { TypeormStore } from 'connect-typeorm';
import { getRepositoryToken } from '@nestjs/typeorm';
import * as passport from 'passport';
import * as session from 'express-session';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);

  const sessionRepository = app.get(getRepositoryToken(TypeORMSession));

  app.use(
    session({
      secret: process.env.SESSION_SECRET,
      resave: false,
      saveUninitialized: false,
      cookie: {
        // Max age of the cookie is 60 days
        maxAge: 60000 * 24 * 60,
      },
      store: new TypeormStore().connect(sessionRepository),
    }),
  );

  passport.serializeUser(function(user, done) {
    done(null, user);
  });
  
  passport.deserializeUser(function(user, done) {
    done(null, user);
  });

  app.use(passport.initialize());
  app.use(passport.session());

  await app.listen(3000);
}
bootstrap();
swift vector
umbral hare
#

although it uses .env for the private data, how should I reasonably do that?

swift vector
umbral hare
#

of course that's not the question 😄

#

The repo is private

#

I'll just zip it without modules, ok?

#

project uses yarn for packages

swift vector
#

I don't see @nestjs/serve-static in package.json

umbral hare
#

You're joking...

#

nooo

#

wait

#

I'm not even mad

#

I'm disappointed

#

what happened is I installed the package on the upper package.json

#

instead of on the backend

#

issue is fixed and the verdict is I'm dum