I'm using environment variables on my mac to store some sensitive credentials, and trying to access them through Node. I added them into my environment profile with
export VARIABLE_NAME=mySensitiveInfo
When I use echo $VARIABLE_NAME I receive the correct output (my sensitive info).
However, when I am trying to access this same variable in Node with process.env.VARIABLE_NAME and try to print it out on the console, I get an undefined.
Other environment variables appear to be okay though. For example, when I console.log(process.env.FACEBOOK_CALLBACK_URL), it prints the correct value to my console. I added FACEBOOK_CALLBACK_URL a few days ago.
Do I have to restart my machine or something? Does it take a certain time before environment variables become available in Node? The closest answer I've seen on SO is this post, but nobody was able to figure out why it was happening.
process.env.VARIABLE_NAME returns undefined because the Node.js execution environment does not know the newly added VARIABLE_NAME yet. To fix the issue, the Node.js execution environment (e.g. IDE) need to restart.
The following steps can be used to reproduce this issue:
console.log(process.env.VARIABLE_NAME). It will print undefined as expected, as VARIABLE_NAME is not defined yet. Keep the IDE running, don't close it..bash_profile and add export VARIABLE_NAME=mySensitiveInfo in it.source .bash_profile, so that the above export statement will be executed. From now on, whenever system console is opened, VARIABLE_NAME environment variable exists.mySensitiveInfo.undefined.mySensitiveInfo If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With