Nikaple / nest-typed-config

Intuitive, type-safe configuration module for Nest framework ✨
MIT License
204 stars 25 forks source link

Environment Variable Substitution Not Working #496

Closed Nathan-Bernardo closed 6 months ago

Nathan-Bernardo commented 12 months ago

I'm submitting a...

Current behavior

Environment variable substitution defined in .env doesn't seem to be substituted into .env.yaml even when ignoreEnvironmentVariableSubstitution: false for file loader. Config file is defined below

// .env
PORT=9000
LOADER=fileLoader
app:
  somePort: ${PORT:-12345}
  envLoader: ${LOADER}

The current output is

app: 
    somePort: 12345
    envLoader: undefined

Expected behavior

Expected output

app:
    somePort: 9000
    envLoader: fileLoader

Minimal reproduction of the problem with instructions

https://gist.github.com/Nathan-Bernardo/bcb98b2215571223299209b9088489f2

Environment


Nest version: 8.0.0
Nest-Typed-Config version: 2.9.1


For Tooling issues:
- Node version: 16.18.2  
- Platform: Ubuntu 18.04            
Nikaple commented 11 months ago

Tried your code with latest version, and it's working fine for me:

image

env variable substitution for fileLoader was implemented in v2.9.0, please reinstall the dependencies and try again.

mydnicq commented 10 months ago

I have the same problem. My yml is:

app:
  port: 3006
db:
  dbHost: ${DB_HOST:'localhost'}

And my ConfigModule looks like:

export const ConfigModule = TypedConfigModule.forRoot({
  schema: Config,
  load: fileLoader({ disallowUndefinedEnvironmentVariables: true }),
});

I was expecting at least throwing an error, because DB_HOST is not set or value should default to localhost. But got this returned:

Config {
app: AppConfig { port: 3006 },
db: DbConfig {
    dbHost: "${DB_HOST:'localhost'}",
}

Using the latest version 2.9.2.

Nikaple commented 10 months ago

@taRusty see the test files for substitution reference, it's the same with dotenv-expand and not relevant with this issue. CodeSandbox link: https://codesandbox.io/p/devbox/brave-chatelet-jfd7zl?file=%2Fsrc%2Fconfig.ts%3A25%2C1

image

mydnicq commented 10 months ago

Oh, I had the wrong configuration then. It should be "${DB_HOST:-localhost}" instead"${DB_HOST:'localhost'}". And then, I was also missing ignoreEnvironmentVariableSubstitution: false. Didn't know the default is true. My bad and apologies for this. Thank you for your help.