I'm adding .env to my Node Express app and when I do:
console.log(process.env)
I see some default process env variables which I didn't add, and also the one custom variable I've added to my .env file (TEST_VAR):
{
npm_package_devDependencies_nodemon: '^1.11.0',
npm_config_version_tag_prefix: 'v',
TEST_VAR: '12345'
}
However when on the very next line I do:
console.log(process.env.TEST_VAR)
I get:
undefined
However, running this:
console.log(process.env.npm_package_devDependencies_nodemon)
Returns the expected:
'^1.11.0'
I was able to solve this with:
var envVars = { ... process.env }
console.log(envVars.TEST_VAR)
Which actually output the value set in my .env file.
Can anybody shed some light on why I need to make a copy before I'm able to access the variables that appear to be present?