Nikaple / nest-typed-config

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

Expanding env variables is not working anymore in v2.5.1 #276

Closed ilya-adnymics closed 1 year ago

ilya-adnymics commented 1 year ago

I'm submitting a...


[X] Regression 
[ ] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request

Current behavior

Seems that expanding env variables is not working anymore in v2.5.1. Setting the expandVariables: true in module config for dotenvLoader gives error

config = dotenvExpand({ parsed: config }).parsed!;
                 ^
TypeError: dotenvExpand is not a function

Reverting to v2.4.7 helped solving the issue.

Expected behavior

Expanding env variables works as described in documentation.

Minimal reproduction of the problem with instructions

This should be enough to reproduce. Create a .env file with following contents:

foo=123
bar=${foo}456

Create a simple config file:

// config.ts
export class AppConfig {
  foo: string;
  bar: string;
}

In your app.module.ts file configure the NestTypedConfig:

// app.module.ts
import { TypedConfigModule, dotenvLoader } from 'nest-typed-config';
import { AppConfig } from './config';
... // other imports from nestjs

@Module({
  imports: [
    TypedConfigModule.forRoot({
      schema: AppConfig,
      load: dotenvLoader({
        expandVariables: true,
      }),
    })
  ],
})
export class AppModule {}

Start the project and the error will occur.

What is the motivation / use case for changing the behavior?

Environment


Nest version: 9.2.0
Nest-Typed-Config version: 2.5.1


For Tooling issues:
- Node version: 18.14.1  
- Platform:  Ubuntu

Others:

Nikaple commented 1 year ago

Unfortunately, I can't reproduce the issue with the code you provided. Please provide a minimum reproduction repo with github or CodeSandbox.

The functionality to expand variables is covered in unit tests: https://github.com/Nikaple/nest-typed-config/blob/main/tests/e2e/dotenv.spec.ts#L147 https://github.com/Nikaple/nest-typed-config/blob/main/tests/src/.expand.env

And latest unit test result seems fine too: https://github.com/Nikaple/nest-typed-config/actions/runs/4423478314/jobs/7756255285#step:8:15

Note: Upgrading nest-typed-config from v2.4.7 to v2.5.1 will bump the dependencies from dotenv@6 and dotenv-expand@5 to dotenv@16 and dotenv-expand@10, maybe updating the dependencies in your project will help.

ilya-adnymics commented 1 year ago

Thank you for looking into it! I did exactly the steps to reproduce here: https://stackblitz.com/edit/nestjs-typescript-starter-othd9g and get the same error.

I didn't have neither dotenv nor dotenv-expand dependencies before nest-typed-config, so they come together and are on the versions you mentioned.

If this might help, the error for me was that in transpiled JS code in v2.5.1 the dotenvExpand had an expand method on it, i.e. it seems like an import/require issue.

I think this might be it: Breaking Move default export to export of expand function, happened in v6 of dotenv-expand.

ilya-adnymics commented 1 year ago

Also, the unit tests are passing because the actual version of dotenv-expand that gets installed is 5.1.0. I.e. somehow version from optionalDependencies "beats" the one from devDependencies, and the tests pass. If you bump version in optionalDependencies, test will fail.

Nikaple commented 1 year ago

Thanks for your contribution! Closed by #278