0

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?

LennonR
  • 1,433
  • 18
  • 35

1 Answers1

0

dotenv requires .env files to be in a specific format, and it doesn't include JSON.

It look more like VARIABLE_KEY=VARIABLE_VALUE. In your case it will look like this

npm_package_devDependencies_nodemon=^1.11.0
npm_config_version_tag_prefix=v
TEST_VAR=12345
molamk
  • 4,076
  • 1
  • 13
  • 22
  • Sorry, if my post was unclear, my .env file contains only a single line that looks like this: `TEST_VAR=12345` The JSON looking object you're seeing is the result of running the line: `console.log(process.env)` – LennonR Feb 13 '19 at 19:24