it-gorillaz / configify

NestJS Config on Steroids
MIT License
61 stars 4 forks source link

Choose .env file depending on environment #55

Open AbdallahSabri opened 3 days ago

AbdallahSabri commented 3 days ago

in the nest application, I have the following files .env.dev, .env.staging, .env.testing, .env.prod, the system should choose which one depends on the NODE_ENV value I noticed it always chooses the .env file, not the file path I provide. Is there anything wrong with the configuration?

import { Module } from '@nestjs/common';
import { ConfigifyModule } from '@itgorillaz/configify/dist';
import { AppController } from './app.controller';

@Module({
  imports: [
    ConfigifyModule.forRootAsync({
      configFilePath: `../.env.${process.env.NODE_ENV}`,
    }),
  ],
  controllers: [AppController],
})
export class AppModule {}
tommelo commented 2 days ago

@AbdallahSabri the module checks for the file extension in order to know how to parse the file since it supports .json, .yml, and .env.

Since you are using .dev, .staging and so on, these files will be excluded from the list of supported config files.

Since I don't know your use case, it's a bit hard to recommend what to do. If you're deploying the app in multiple different environments I guess you can simply configure different values for the environment variables instead of using multiple files.

AbdallahSabri commented 2 days ago

@tommelo I'll give a database as an example; if the env is development, take the db connection from the .env.dev file; it is a production env, read the .env.prod file, and so on

tommelo commented 2 days ago

@AbdallahSabri so, if I understood correctly you're deploying the env files to different environments, like dev, prod and so on. If my assumption is correct, you could simply configure the system's environment variables instead of deploying .env files, the module also reads environment variables by default(unless is disabled in the config options).

Anyway, could you try testing it with different file names? E.g.: Instead of .env.prod, try .prod.env. Same pattern to the other environments.

The module checks for the file extension, in your case your extensions are: .dev, .staging, .prod which are not recognizable by the file parsers.

In order to a file named .env.prod to work, we would have to change the module and allow passing a file type to the config to identify which parser should read and parse the file.

AbdallahSabri commented 1 day ago

@tommelo It does not work either. It looks like the attribute configFilePath is not working. I'm not sure how to validate it.

tommelo commented 1 day ago

@AbdallahSabri I tried to reproduce the scenario that you described but I it seems is picking up the correct files. Please check: https://github.com/tommelo/test-multiple-configify-envs

After cloning the project:

npm install

Then define your environment:

export NODE_ENV=dev

Access http://localhost:3000 and you should see a message DEV_ENVIRONMENT. Stop the application, change the env var: export NODE_ENV=staging and run it again. You should see a staging message this time. Same process for prod: export NODE_ENV=prod.

It seems to be picking up the correct files.