Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ENV Variable Equivalent in Javascript

Before you flame me, I've done my research (Javascript ENV variables). I know that it is not possible to access system environment variables using Javascript.

I'm using Yeoman to develop a Javascript library. That library is for use with Google Maps API and some of my tests require that Google Maps API as a dependency. To load the Google Maps API script, you need an API key. Now, my code is located on my Github and I really wouldn't like to have my API key as part of the code.

Is there any Node module that would be able to inject an environment variable into my tests when running Grunt? Are there any ENV variable equivalents in Node.js?

This is an example of a need for a JS ENV variable solution. There are other ways around the problem, but I am specifically looking for this type of a solution.

Edit: most importantly, how do I have these ENV variables auto load into my program when I run tests using grunt

Thank you!

like image 854
connorbode Avatar asked Jan 23 '14 22:01

connorbode


People also ask

What is environment variable in JavaScript?

Environment variables store variables, as the name implies. The values or things that get assigned to these variables could be API keys that you need to perform certain requests or operations. To create an environment variable, all you need to do is create a new file called .

What can I use instead of environment variables?

Shared data source as an alternative to environment variables. If you find yourself needing to change these values often, we recommend a shared data source, such as a parameters table on your existing DB; if you need something faster, we recommend a Redis or Riak source.

Can JavaScript read environment variable?

To retrieve environment variables in Node. JS you can use process. env. VARIABLE_NAME, but don't forget that assigning a property on process.


1 Answers

Yes, you can access the environment variables in a Node script via the process object.

console.log(process.env.secret_key);

Storing your API keys in environment variables is much better than doing so in your repo.

like image 62
Daniel Avatar answered Oct 20 '22 10:10

Daniel