NodeJS + ExpressJS, unable to read environment variable

express, node.js

Solution

`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';

Problem

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`?

Original source