Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use .env file variable in .js file other than Keystone.js?

We are using keystone frame work in one of our project and i am trying to use .env file variable to one of my .js file to connect with http site.I have used dotenv and called process.env.xxyz where xxyz is the variable we are using.Please let me know if there is any other method to call variable from .env file.

like image 213
jugal Avatar asked Jan 06 '17 07:01

jugal


2 Answers

Reading process.env is the standard way to retrieve environment variables. See the docs.

The dotenv package you mentioned takes what is in your .env file and puts it in process.env. However, no validation is performed, so it is easy to make mistakes.

Instead, try out my envy module which prevents all of the common mistakes you might be making. It checks for missing variables, amongst other things.

If .env files are still giving you trouble:

  • Make sure you are using the correct variable names.
  • Make sure there are no typos.
  • Make sure you are using the proper syntax.
  • Consider using command line arguments instead. I recommend meow for this. Also see an example of using envy and meow together.

Regarding syntax: depending on which loader you are using (e.g. dotenv vs envy), the syntax in the .env file could have a big impact on how it is parsed. For example, in Bash and other shells (which the files are based on), each and every one of the following examples behaves entirely differently...

MY_VAR=foo $BAR
MY_VAR='foo $BAR'
MY_VAR="foo $BAR"

Also, environment variable names are case sensitive and by convention are all uppercase. This can lead to mistakes in languages where that is uncommon. In the Node.js program reading process.env, sometimes you might forget that the naming conventions for environment variables are different than the rest of the program.

const myVar = process.env.myVar;   // wrong
const myVar = process.env.MY_VAR;  // correct

Casing is not an issue if you use envy, as it fixes this by normalizing variable names to camelcase before returning them.

const { myVar } = envy();  // correct, no matter how it is in `.env`

Regardless of which loader you use, you will of course need to call its load function before the .env file will be read. This is pretty much impossible to forget with envy() because you use only its direct return value. But if you are using dotenv, you can easily access process.env at the wrong time because it is already available and populated (but not with all the desired properties) before calling dotenv.config().

Another trick that will help with debugging and save time and effort is to make a dedicated module for configuration. Thanks to the require cache, we avoid doing the work multiple times and also avoid relying upon the loader being idempotent.

Put this in a file called env.js.

const envy = require('envy');
module.exports = envy();

Then import it elsewhere.

const env = require('./env');

Now you have something very simple to debug and it should work the same no matter where you import it.

like image 98
Seth Holladay Avatar answered Oct 13 '22 01:10

Seth Holladay


Just add xxyz=HTTPSiteAddress to your .env file. Then you can call the variable anywhere by using process.env.xxyz.

For example:

var request = require("request");

request(process.env.xxyz, function(error, response, body) {
  console.log(body);
});

This will work as long as your keystone.js file contains at the top:

require('dotenv').config(); 
like image 40
Chris Avatar answered Oct 13 '22 02:10

Chris