When it comes to my testing framework, I rely on dotenv for handling environment variables across different test environments. Currenty, I'm looking for a way to incorporate the value of a specific environment variable into both the value and name of another environment variable.
To achieve this, I've set the environment variable export INSTANCE=1
prior to initializing the dotenv file.
Below is the snippet from my env file where I define the environment variables to be initialized using dotenv
:
# Using INSTANCE in the value of an env var
MY_DOMAIN="http://mypage/"$INSTANCE""
# Using INSTANCE in the name of an env var
SUBDOMAIN"$INSTANCE"="/mysubdomain"
Unfortunately, the code I attempted below didn't work as expected:
# Using INSTANCE in the value of an env var
MY_DOMAIN=http://mypage/${INSTANCE}
# Using INSTANCE in the name of an env var
SUBDOMAIN${INSTANCE}=/mysubdomain
I was able to successfully achieve this through bash with the following commands:
export MY_DOMAIN="http://mypage/"$INSTANCE"" SUBDOMAIN"$INSTANCE"="/mysubdomain"
echo $MY_DOMAIN
http://mypage/1
echo $SUBDOMAIN1
/mysubdomain
However, when utilizing dotenv
, the same approach did not yield the desired outcome. Below is the code snippet:
const dotenv = require("dotenv");
var result = dotenv.config({ path: '/path/to/my/env/file' });
console.log(result.parsed);
The output of the console.log
displays:
{
MY_DOMAIN:'http://mypage/"$INSTANCE"'
}
It seems like the environment variable INSTANCE
is being ignored. Is there any workaround or solution that can help me resolve this issue?