Search code examples
node.jsreactjsherokuecmascript-6create-react-app

accessing heroku config var in my React app


I have defined a config var "BASE_URL" in my Heroku app and I'm trying to access it in my React app as

process.env.BASE_URL

but it gives me undefined when I console.log it as it seems to be not existing. How can I access Heroku config vars in my React app?


Solution

  • You have to use a module that loads environment variables. Then,

    require('dotenv').config()
    ...
    console.log(process.env.BASE_URL);
    

    Check this post, pretty useful.