Nest 实例在生产环境启动时抛出不完整的导入错误。

huangapple go评论66阅读模式
英文:

Nest instance throw incomprehensive import errors on start in production

问题

我有一个使用 NODE_ENV=production nest build 构建的nestjs应用程序。
在我的MacBook上,使用 NODE_ENV=production nest start 命令启动它,同时也可以使用本地Vagrant虚拟机上的生产服务器实例运行相同的命令启动。
但是,在同样使用Node版本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)

我甚至通过scp传输了我本地构建的dist和node_modules文件夹,并调整了所有权,但仍然没有运行成功。

我的app.module文件如下:

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("*");
  }
}
英文:

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("*");
  }
}

答案1

得分: 0

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 {},
]
英文:

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 {},
]

huangapple
  • 本文由 发表于 2023年6月13日 04:02:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76459943.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定