How can I conditionally check if React app is in 'production' or 'dev' in a Heroku environment?

37 Views Asked by At

Here is the code:

if (process.env.NODE_ENV === 'production') {
  module.exports = require('./keys_prod');
} else {
  module.exports = require('./keys_dev');
}

keys_prod IS on the server

keys_dev is only on my local machine

Here is the problem:

when the app starts up on Heroku I get the following:

ERROR in ./src/environment/keys.js 8:2-40
 Module not found: Error: Can't resolve './keys_dev' in '/app/src/environment'

Heroku Config vars

I seem to remember reading somewhere, a long time ago that environment variables in Heroku for a React app needed to be prefixed with REACT_APP in order for the React app to pick them up... but that doesn't seem to be the case now.

I have the following set in the Heroku config vars:

enter image description here

package.json

my package.json scripts section

  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject",
    "heroku-postbuild": "npm run build"
  },

saw this but I am on heroku 22 - so that won't work

Try 1

It was suggested to set NODE_ENV to production...

enter image description here

Try 1 - Result

ERROR in ./src/environment/keys.js 8:2-40
Module not found: Error: Can't resolve './keys_dev' in '/app/src/environment'
2

There are 2 best solutions below

3
user229044 On

You're setting REACT_APP_NODE_ENV, but not NODE_ENV. Set NODE_ENV to production.

2
MrLister On

THIS works

if (process.env.REACT_APP_NODE_ENV === 'production') {
  module.exports = require('./keys_prod');
} else {
  module.exports = require('./keys_dev');
}