0

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

andymel
  • 4,538
  • 2
  • 23
  • 35

1 Answers1

0

You are nothing doing wrong unless you don't wait for NestFactory to create your app:

// main.ts

async function bootstrap() {
  const app = await NestFactory.create(AppModule);

  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'));

  await app.listen(3000);
}
bootstrap();

or maybe you might need clean artifacts on your local machine. Your latest changes might be needed to be evaluated again:

rm -rf ./dist
cagcak
  • 3,894
  • 2
  • 21
  • 22
  • Thanks. Tried deleting dist wihout success. From the logs I can tell, that I use the configService later. Found out, that the options that I set in `ConfigModule.forRoot({...})` don't work. I tried `envFilePath: 'test.env'` in addition to `expandVariables: true` and it still tries to load the default `.env` file. Any idea? – andymel Mar 27 '23 at 21:42
  • Another finding: it works on the linux server (deployed as a node/alpine docker container running on a debian 10 server). My dev machine is windows where it still does not work. – andymel Mar 27 '23 at 23:42
  • seems like there are some windows specific filesystem/pemission issues occuring. sometimes windows locks certain files which are busy at the moment hooked by unterminated processes. first, ensure the root directory of the project has owned, recursively, by the user in use. then ensure your .env file besides to package.json. by default, config module looks for .env file in the root directory of the app. if it can't, it is possibly installed wrongly. try deleting all node_modules and reinstall them all again, if the docker container runs with own isolated installation of node_modules of course – cagcak Mar 28 '23 at 07:38