Can't read environment variables from a Grunt server-run Node + AngularJS app

Viewed 404

I've created an app on AngularJS and Firebase. I decided to store the Firebase credentials as environment variables. I am on Ubuntu, and I run the development server using Grunt.

I tried creating a .bashrc file to store the environment variables as shown below (with the values inserted between the quotes).

export APIKEY=""
export AUTH_DOMAIN=""
export DATABASE_URL=""
export STORAGE_BUCKET=""
export MESSAGING_SENDER_ID=""

I then have a config.js file that uses these variables to configure Firebase.

var config = {
    apiKey: process.env.APIKEY,
    authDomain: process.env.AUTH_DOMAIN,
    databaseURL: process.env.DATABASE_URL,
    storageBucket: process.env.STORAGE_BUCKET,
    messagingSenderId: process.env.MESSAGING_SENDER_ID
};

firebase.initializeApp(config);

However, I think Grunt does not pick up these values, because whether I run the Grunt server or run the app using node server.js, it doesn't find a value for these variables. I also tried creating the variables from the command line first, like so (with the value inserted between the quotes).

$ export APIKEY=""

Regardless, my app is unable to see the environment variables. It delivers this error in the console.

Uncaught ReferenceError: process is not defined at config.js:2

The app runs fine when I insert the values directly into the config.js file. I tried a variety of options for configuring variables in Ubuntu, and none seem to be working.

Edit: It also might be worth noting that Heroku also does not recognize the variables, though I have registered them with Heroku, and it delivers the same error (process is not defined). Could it be something with Angular?

1 Answers

You'd need to create a file env.js that contains your credentials and require in your config.js file. You can ignore the file from Git.

env.js

module.exports = {
  APIKEY: "your-api-key",
  AUTH_DOMAIN: "your-auth-domain",
  DATABASE_URL: "your-database-URL",
  STORAGE_BUCKET: "your-storage-bucket",
  MESSAGING_SENDER_ID: "your-messaging-sender-id"
}

.gitignore

env.js

config.js

var keys = require('env');

var config = {
  apiKey: keys.APIKEY,
  authDomain: keys.AUTH_DOMAIN,
  databaseURL: keys.DATABASE_URL,
  storageBucket: keys.STORAGE_BUCKET,
  messagingSenderId: keys.MESSAGING_SENDER_ID
};

firebase.initializeApp(config);
Related