英文:
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 {},
]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论