motdotla / dotenv-expand

Variable expansion for dotenv. Expand variables already on your machine for use in your .env file.
https://dotenvx.com
BSD 2-Clause "Simplified" License
934 stars 93 forks source link

Inconsistent Behavior with Default Environment Variable Values Using dotenv and dotenv-expand #125

Open GeryDeMerciYanis opened 5 months ago

GeryDeMerciYanis commented 5 months ago

Hello,

I've encountered an issue regarding the handling of default environment variable values when using dotenv in conjunction with dotenv-expand.

The goal is to access an environment variable with the option to fall back to a default value if the variable is not defined. To achieve this, my .env configuration is as follows: MY_VAR=${MY_VAR:-default-value}

Upon setting up dotenv and dotenv-expand and subsequently accessing process.env.MY_VAR, the output unexpectedly matches the literal string in the .env file: ${MY_VAR:-default-value}.

However, altering the .env file to use a non-existent variable as the key in the fallback expression: MY_VAR=${NOT_MY_VAR:-default-value} Results in the correct behavior, yielding default-value when process.env.MY_VAR is accessed.

I've prepared a minimal reproducible example available at: CodeSandbox - Node.js Playground Forked.

Environment:

Node.js version: 20.11.0 dotenv version: 16.4.5 dotenv-expand version: 11.0.6

It appears that dotenv-expand does not process the default value syntax as expected when the variable name is the same as the one being defined. I'm looking forward to any insights or solutions regarding this issue.

Thank you.

stvvt commented 3 months ago

I've noticed exactly the same behaviour and proposed https://github.com/motdotla/dotenv-expand/pull/126 to fix it.