Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NodeJS + ExpressJS, unable to read environment variable

I am setting a variable in Express.js as follows: app.set('HOST', 'demo.sample.com');. However, if I try to read this variable, I get undefined as the output. I am reading the variable using process.env.HOST. Nevertheless, if I try to read it using app.get('HOST'), I get the correct value.

I cannot use app.get('HOST') since I am reading the variable in another file too — a file that does not contain a reference to the Express.js app variable.

How do I get the value using process.env.HOST?

like image 771
user109187 Avatar asked Apr 11 '13 08:04

user109187


1 Answers

app.set() doesn't set environment variables, it's just a convenience method offered by Express to be used together with app.get().

If you want to set an environment variable in your JS code, use this:

process.env.HOST = 'demo.sample.com';
like image 157
robertklep Avatar answered Oct 05 '22 07:10

robertklep