What is the correct way to use .env with multi-stage config files?

amazon-alexa

#1

Is the following, the correct way to do this?

NOTE: The call to dotenv in config.local.js

config.js

module.exports = {
    custom: {
        myApiKey: process.env.MY_API_KEY
    }
 };

config.local.js

    require('dotenv').config();

    module.exports = {}

config.prod.js

module.exports = {}

app.js

const key = this.$app.$config.custom.myApiKey

.env

STAGE = "local"
MY_API_KEY = "KEY1"

When stage is ‘local’, the value from the .env file will be used. When the state is ‘prod’, the value from the lambda environment variable will be used.

Thanks,

Mark


#2

Hi Mark,

As only the exported object from config.local.js is merged into the default config.js, I’m not sure if requiring dotenv there works.

Did you run into any problems?