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');

Create a .env and put some vars in it:


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