I’m trying to use dotenv
to load the following .env file:
PORT=4000
BASE_URL=http://127.0.0.1:${PORT}/
require('dotenv').config('.env')
For some reasons, dotenv
does not resolve ${PORT}
to 4000
, instead it just returns a plain string like this:
{
parsed: {
PORT: '4000',
BASE_URL: 'http://127.0.0.1:$PORT/',
}
}
How can I make dotenv
read variables inside .env
file?
2
Answers
You can use dotenv-expand because dotenv doesn’t support variable substitution using
${}
syntax.to use
dotenv-expand
installadd require dotenvExpand keep the same
.env
file with${}
syntaxThe output will return like:
Output:
try to access to your variable with
process.env
for exemple
process.env.PORT