6

I tried to set some env. variables on Heroku and I did, I can see the variables in the app when opening the Heroku dashboard. But When I try to access the variables from my app it return undefined any idea?

componentDidMount() {
    console.log('this env' , process.env.backEndServer) // undefined
}

checking in the Heroku cli:

$ heroku config:get backEndServer // https://myserver.com
7
  • 1
    Interesting. Does this help? stackoverflow.com/questions/47561604/… Commented Jul 31, 2018 at 19:44
  • 1
    Also, are other environment variables showing up in process.env? Commented Jul 31, 2018 at 19:46
  • 2
    @SudhanshuVishnoi I googled the hell of it and you simply cant access process.env from the client side which is where I tried to access it Commented Jul 31, 2018 at 19:49
  • Are you using react to render on server side? Commented Jul 31, 2018 at 19:50
  • 1
    @SudhanshuVishnoi no I use react but rendering client side Commented Jul 31, 2018 at 19:51

2 Answers 2

3

You're trying to access the environment variable on client side and this is not possible.

If you want to use environment variables on your client side application you need to set up Webpack to handle different environments.

In Webpack config files, you’ll define your global variables for each environment using Webpack’s Define plugin.

Also don't forget to add NODE_ENV config variable to your heroku app and set it to true. So you'll be sure that by accessingprocess.ENV.NODE_ENVwill force runtime to usenode.js` environment.

Now you can configure you're production environment as following:

/* in webpack.config-prod.js */
...,
plugins: [    
  new webpack.DefinePlugin({           
    NODE_ENV: JSON.stringify(process.env.NODE_ENV),      
    API_HOST: JSON.stringify(process.env.API_HOST)
  })
],
...

Now you can easily access you're environment variables in your client side app.

Sign up to request clarification or add additional context in comments.

Comments

0

If you check the Heroku build log, it recommends that you specify a nodejs version. If you specify a node version in your package.json, it might fix the problem.

package.json

{
...
  "engines": {
   "node": "12.13.1"
   },
...
}

1 Comment

no good, this didn't do any good for me. ENV variables are still missing

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.