Sails.js Environment Config Variables

So Sails.js way of handling environment variables is horrifying.

Everything in config/local.js and config/env sort of do their job, but it’s not enough for environment variables.

What we really need is a place to store all of our config variables regardless if we are in a development, testing, or production environment. The answer is simple: use dotenv. This will load a .env file or look for environment variables on the machine.

Start by pulling it into the Sails.js project and following the dotenv docs:

$ npm install dotenv --save

Then require dotenv in app.js and load the .env file:

var dotenv = require('dotenv');
dotenv.load();

Create a .env and put some vars in it:

CLIENT_ID=enfiownqefniqofewqofnieqwvewlk
CLIENT_SECRET=123456789

Now you can access those environment variables anywhere in the app (probably in some config/ files):

console.log(process.env.CLIENT_ID);
console.log(process.env.CLIENT_SECRET);