How do I pass environment variables from bashrc to Ember CLI. I imagine a situation where you need stripe api keys or pusher api-keys and you have them in your environment variables in bashrc. How do you pass the api-keys to Ember CLI.
I tried using Node.js process.env
in both the brocfile.js
and environment.js
, but when I try to access it in the Ember JS controller, the property is null.
In my environment.js
file I added,
APP: { apiKey: process.env.KEY }
In My Ember JS controller I tried accessing it with:
import config from '../config/environment';
And setting the controller property lkey
as shown below, which didn't work:
lkey: config.App.KEY
Next in my brocfile.js
, I added:
var limaKey = process.env.Key; var app = new EmberApp({key: limaKey});
This still didn't work.
Environment variables are excellent places to store sensitive information (such as API keys), or information that needs to be globally accessible from anywhere in the app, (such as version numbers or hardcoded paths).
Environment variables are character strings of the form "name=value". There are two types of environment variables: Job-level environment variables. The job-level environment variables are stored in an environment space outside of the program associated with the job.
Ember CLI, Ember's command line interface, provides a standard project structure, a set of development tools, and an addon system. This allows Ember developers to focus on building apps rather than building the support structures that make them run.
I finally resolved this issue. I was faced with two options. Option 1 was to use XHR to fetch the api-keys from an end-point on the server. Option 2 is get the api-key directly from environment variables using Nodejs process.env. I prefer option 2 because it saves me from doing XHR request.
You can get option 2 by using this ember-cli-addOn which depends on Nodejs Dotenv project
In my case I choose to do it without any addOn.
.bashrc
if you are Ubuntu or the approapriate place for your own linux distro.export API_KEY=NwPyhL5
.bashrc
file, so your setting are picked up:source ~/.bashrc
ENV
object in config/environment.js
. The default looks like thismodule.exports = function(environment) { var ENV = { modulePrefix: 'rails-em-cli', environment: environment, baseURL: '/', locationType: 'auto', EmberENV: { } }
Now to that ENV
object, we can add a new property myApiKey like this:
module.exports = function(environment) { var ENV = { modulePrefix: 'rails-em-cli', environment: environment, baseURL: '/', locationType: 'auto', myApikey: null, EmberENV: { } //assign a value to the myApiKey if (environment === 'development') { // ENV.APP.LOG_RESOLVER = true; ENV.myApiKey = process.env.API_KEY; } }
Note that process.env.API_KEY is fetching the setting we added to .bashrc
and assigning it to myApiKey. You will need to have Nodejs installed on your server for process.env to work.
Finally to access that variable in your controller you do
import config from '../config/environment'; import Ember from 'ember'; export default Ember.Controller.extend({ yourKey: config.myApikey, });
That's it.
You can also set the variables on the ENV.APP
object: they will be carried by the application instance.
You can then reuse them inside initializer & so on.
This way, you won't have to import config/environment
into application's code, which seems a little weird to me.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With