NestJS ConfigService在应用程序启动时的异步TypeForm配置期间返回未定义的值
我正试图通过以下一些文档为我的NestJS应用程序设置配置: 配置: 字体: 我已使用以下值将.env文件添加到项目的根目录中(与NestJS ConfigService在应用程序启动时的异步TypeForm配置期间返回未定义的值,nestjs,nestjs-config,Nestjs,Nestjs Config,我正试图通过以下一些文档为我的NestJS应用程序设置配置: 配置: 字体: 我已使用以下值将.env文件添加到项目的根目录中(与package.json的级别相同): DB_URL=localhost DB_USER=root DB_PASSWORD=root DB_NAME=test_db 在我的app.module.ts中,我导入以下模块: import { Module } from '@nestjs/common'; import { TypeOrmModule } from '@n
package.json
的级别相同):
DB_URL=localhost
DB_USER=root
DB_PASSWORD=root
DB_NAME=test_db
在我的app.module.ts中,我导入以下模块:
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { AppController } from './app.controller';
import { AppService } from './app.service';
@Module({
imports: [
ConfigModule.forRoot(),
TypeOrmModule.forRootAsync({
imports: [ConfigModule],
useFactory: (configService: ConfigService) => {
// The value logged below is undefined'
console.log(`DB URL: ${configService.get('DB_URL')}`);
return {
type: 'mysql',
host: configService.get('DB_URL'),
port: 3306,
username: configService.get('DB_USER'),
password: configService.get('DB_PASSWORD'),
database: configService.get('DB_NAME'),
entities: ['dist/**/*.entity{.ts,.js}'],
synchronize: false,
migrations: ['dist/migrations/*{.js}'],
cli: {
migrationsDir: 'migrations',
},
};
},
inject: [ConfigService],
}),
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
如上所述,configService.get('DB_URL')检索到的值是
未定义的
。希望获得有关如何设置配置的任何帮助/建议,以便在启动应用程序时可以读取.env
文件中的值。注意:此配置似乎正确地在Mac上加载了.env文件和环境变量。运行此问题的我的操作系统是Windows 10。我尝试在ConfigModule选项中指定envFilePath
,甚至尝试直接使用dotenv
文件。因此,这似乎是一个更广泛的问题,因为我的操作系统没有正确解析和设置我的.env
文件中的值。