How to access the .env variables inside javascript?
I am currently using algolia so whenever a record is created it is also send to the agolia database. Ofcourse I want to seperate my data in the algolia database with a testset for local development and a production set. I have defined which indices of my algolia database are being used in a javascript file.
Now I am wondering how do I react accordingly to my APP_ENV variable to change it depending on my current environment? Obviously I need to put stuff into an if statement but how do I make my javascript access the .env variables properly?
@stephan-v You can’t access environment variables from client-side JavaScript. Being able to do so would be a security risk (since that’s where sensitive things like database credentials are stored).
Instead, consider having two instances: a “production” instance and a development instance. This is what you should do with things like S3 buckets etc.
@stephan-v @martinbean This is not entirely true. You can use dotenv package to parse .env file and pass result data to webpack via DefinePlugin which will replace specified values (i.e. APP_URL, FACEBOOK_CLIENT_KEY etc.) in your bundled client-side code with literal values. All other values inside your .env file will remain private. So in fact, you can use single .env file on both front-end and back-end in a secure and intuitive way.
Here is the gist with working setup. Note, that there are two files there. The first one just abstracts the logic behind the parsing and validation of .env file. The second is development version of webpack.config.js.
Actually, I think @JeffreyWay should take note of the above method. It may be possible to apply this stuff to new versions of Elixir that use Webpack (or at least I've heard it would).
You are correct but if your checking on local/dev yes but in production that check better error out with access issues. As your env file better be inaccessible.
Anyways, I set a url check via JavaScript. If url is localhost (or whatever) then use this, if not then it's on a production server and use this or the other way around to fit many devs.
I personally really, really hate mixing php in JavaScript. There are was around it and seems just lazy to couple the two like that.
@jekinney I don't really see a difference. Webpack doesn't insert .env file into your javascript, it only replaces requested(!) variables with their values in the output (i.e. APP_URL becomes http://localhost). Which means the chances you'll get your DATABASE_PASSWORD and the like shown in production javascript code is pretty much non-existent unless you do it in purpose.
Also, it's only logical to store your environment variables in one place when you can instead of sprinkling them around your codebase.
@jekinney You forget that OP is not the only one who will use this thread. People search for stuff in Google and this will later be used as the reference answer for the question on how to get access to environment variables. I'm just being helpful here. I too had this question at some point (forgot the reason though, it seems like I used credentials for some external API in both places). By the way, APP_URL was used for an example purposes, replace it with whatever you like. But I can tell you now that you WILL need this exact URL in webpack config (not your app code where you can just read it from location object) to do any semi-complex build with dynamically loaded chunks of data.