node.jsnestjsdebianproduction

Nest instance throw incomprehensive import errors on start in production


I have a nestjs app that i build using NODE_ENV=production nest build. It starts on my macbook using the NODE_ENV=production nest start command, it also starts with the same command using the local vagrant box instance of our prod machine. But it throws this on the prod machine running the same node version 16.20:

[Nest] 1108076  - 06/12/2023, 9:54:51 PM     LOG [NestFactory] Starting Nest application...
[Nest] 1108076  - 06/12/2023, 9:54:51 PM   ERROR [ExceptionHandler] Nest cannot create the AppModule instance.
Received an unexpected value at index [3] of the AppModule "imports" array.

Scope []
Error: Nest cannot create the AppModule instance.
Received an unexpected value at index [3] of the AppModule "imports" array.

Scope []
    at DependenciesScanner.scanForModules (/home/check-inventory/test/node_modules/@nestjs/core/scanner.js:56:23)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async DependenciesScanner.scan (/home/check-inventory/test/node_modules/@nestjs/core/scanner.js:27:9)
    at async /home/check-inventory/test/node_modules/@nestjs/core/nest-factory.js:107:17
    at async Function.asyncRun (/home/check-inventory/test/node_modules/@nestjs/core/errors/exceptions-zone.js:22:13)
    at async NestFactoryStatic.initialize (/home/check-inventory/test/node_modules/@nestjs/core/nest-factory.js:106:13)
    at async NestFactoryStatic.create (/home/check-inventory/test/node_modules/@nestjs/core/nest-factory.js:42:9)
    at async bootstrap (/home/check-inventory/test/dist/src/main.js:13:17)

I even transferred my locally built dist and node_modules via scp and adjusted all the ownerships, with no luck still...

My app.module file:

import { MiddlewareConsumer, Module, NestModule } from "@nestjs/common";
import { AppController } from "./app.controller";
import { AppService } from "./app.service";
import { TypeOrmModule } from "@nestjs/typeorm";
import { ConfigModule } from "@nestjs/config";
import { UsersModule } from "./users/users.module";
import { GroupsModule } from "./groups/groups.module";
import { RequestsModule } from "./requests/requests.module";
import { CategoriesModule } from "./categories/categories.module";
import { ItemsModule } from "./items/items.module";
import { UserInjectMiddleware } from "./middleware/user-inject.middleware";
import { TrackerModule } from "./tracker/tracker.module";
import { ScheduleModule } from "@nestjs/schedule";
import { SerialsModule } from "./serials/serials.module";
import { SocketModule } from "./socket/socket.module";
import { StoragesModule } from "./storages/storages.module";
import { AuthModule } from "./auth/auth.module";
import { APP_GUARD } from "@nestjs/core";
import { AzureValidatorGuard } from "./auth/azure-validator.guard";
import { RolesGuard } from "./auth/roles.guard";
import { StatisticsModule } from "./statistics/statistics.module";
import { config } from "./dataSource.config";

const env = process.env.NODE_ENV || "production";

@Module({
  imports: [
    ConfigModule.forRoot({
      isGlobal: true,
      envFilePath: `../.env.${env}`,
    }),
    TypeOrmModule.forRoot(config),
    ScheduleModule.forRoot(),
    process.env.EASYPOST_APIKEY ? TrackerModule : null,
    UsersModule,
    GroupsModule,
    RequestsModule,
    CategoriesModule,
    ItemsModule,
    SerialsModule,
    StoragesModule,
    SocketModule,
    AuthModule,
    StatisticsModule,
  ],
  controllers: [AppController],
  providers: [
    AppService,
    {
      provide: APP_GUARD,
      useClass: AzureValidatorGuard,
    },
    {
      provide: APP_GUARD,
      useClass: RolesGuard,
    },
  ],
})
export class AppModule implements NestModule {
  configure(consumer: MiddlewareConsumer) {
    consumer.apply(UserInjectMiddleware).forRoutes("*");
  }
}

Solution

  • the imports array shouldn't have that null element (nor undefined). That's why.

    Instead, if you really want to use the ternary operator like that, you could do:

    imports: [
      process.env.EASYPOST_APIKEY ? TrackerModule : class NoopModule {},
    ]