2

I have a problem accessing the environment variables in my EmberJs app.

I want to use the environment variable set in the .profile file of the server.

My .profile in the server

export SERVER_NODE=testNode

I already tried this solution :

  • process.env like node.js in my config/environment.js but it doesn't work, i cannot access it in my app (Stack Post here)

So if you have any idea about how I can do this, I take it.

Thanks you.

Community
  • 1
  • 1
Erwan Maro
  • 53
  • 7

1 Answers1

3

Try using the ember-cli-dotenv Ember addon: https://github.com/fivetanley/ember-cli-dotenv

This will let you access variables from a .env file in config/environment.js. You can then access these variables from a component or service by importing config/environment.js.

Example:

.env

CLIENT_KEY=THEKEY
CLIENT_SECRET=SECRETSECRET

config/environment.js

module.exports = function(environment) {
  var ENV = {
    API: {
      clientKey: process.env.CLIENT_KEY,
      clientSecret: process.env.CLIENT_SECRET
    }
  }
};

app/services/example.js

import Ember from 'ember';
import ENV from '../config/environment';

export default Ember.Service.extend({
  clientKey: ENV.API.clientKey, // 'THEKEY'
  clientSecret: ENV.API.clientSecret // 'SECRETSECRET'
});
Mario Pabon
  • 605
  • 4
  • 8