5

In my index.js code, I've checked the value of process.env.NODE_ENV variable and expected it to be 'production', but on Heroku this variable is registering a value of 'development'.

I've read that Heroku sets this value to 'production' by default, but for some reason it's still registering as 'development', even when I explicitly set the value under Config Vars it still shows it's development. Below is the condition in my index.js file:

if (process.env.NODE_ENV === 'production') {
  // serve production assets...
} else {
  // serve development assets...
}

If I change the line to...

if (process.env.NODE_ENV === 'development') {

The production assets get served up just fine on Heroku.

Any ideas on how I may get process.env.NODE_ENV to register as 'production' on Heroku?

EDIT:

Within my webpack.js file I am doing this...

const env = process.env.NODE_ENV;
const config = {
  mode: env || 'development'
};

module.exports = {
  // some code...

  ...config, // 'development' or 'production' mode

  // more code...
}

I did try hard-coding this value to 'production' like so...

mode: env || 'production'

But that had no effect.

mobiman
  • 619
  • 1
  • 11
  • 26

2 Answers2

3

To check if the NODE_ENV variable is set you can open a console in your project enviroment at:

https://dashboard.heroku.com/apps/{{your-app-name}}?web-console={{your-app-name}}

Then run node to execute node code from console (you can run this command in your local repo too):

heroku run node

Once node is running in terminal execute:

console.log(process.env.NODE_ENV) // production

It should return the enviroment variable value of the project

Rashomon
  • 5,962
  • 4
  • 29
  • 67
  • 1
    Thanks, that returns two values... 'production' and 'undefined'. What is the second value for? – mobiman Feb 16 '19 at 15:38
  • Thats a good question. Seems the returned value of the function `log`. https://stackoverflow.com/questions/24342748/why-does-console-log-say-undefined-and-then-the-correct-value – Rashomon Feb 16 '19 at 15:45
  • I am also getting `production` from the Heroku console but the issue still happens. That means this approach of checking the environment does not work. Both variables actually don't match. – blackr1234 May 08 '21 at 05:28
  • Holy cow I never knew about `heroku run node` - AWSM! – bhu Boue vidya Jan 23 '23 at 23:42
1

The problem is with my webpack.js file, specifically this code:

const env = process.env.NODE_ENV;
const config = {
    mode: env || 'development'
};

For some reason process.env.NODE_ENV was being set to "development". I've deleted the config variable and replaced it with this hard-coded value:

mode: 'development'

And now it works. Only problem is I must manually change that value before I deploy to production.

mobiman
  • 619
  • 1
  • 11
  • 26
  • 1
    This isn't really a practical solution. It's basically avoiding the issue. I just encounter the exact same issue. In my case, it must be a valid case of this issue because I am switching from GitHub Pages to Heroku. Things like react-git-info, preval.macro and this `process.env.NODE_ENV` all work when I build with gh-pages locally but none is working when Heroku builds my project in the cloud. – blackr1234 May 08 '21 at 05:19