I try to port a project to NestJs and have some troubles with environment variables.
For now I just have a single .env file in the root of the project. The variables can be retrieved from the ConfigService but they are not expanded.
The App Module
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { SpacesModule } from '@amc/spaces-server';
@Module({
imports: [
ConfigModule.forRoot({
expandVariables: true,
}),
SpacesModule
],
controllers: [],
})
export class AppModule {}
The .env file
APP_URL=mywebsite.com
SUPPORT_EMAIL=support@${APP_URL}
SUPPORT_EMAIL2=support@$APP_URL
In my main.ts
I try to log what is there
const configService = app.get(ConfigService);
Logger.log("a " + configService.getOrThrow('APP_URL'));
Logger.log("s1 " + configService.getOrThrow('SUPPORT_EMAIL'));
Logger.log("s2 " + configService.getOrThrow('SUPPORT_EMAIL2'));
The logged output is
[Nest] 5344 - 27.03.2023, 15:20:33 LOG a mywebsite.com
[Nest] 5344 - 27.03.2023, 15:20:33 LOG s1 support@${APP_URL}
[Nest] 5344 - 27.03.2023, 15:20:33 LOG s2 support@$APP_URL
Any idea what prevents expansion of those variables? My code was inspired by https://docs.nestjs.com/techniques/configuration#expandable-variables