2

There is some peculiar scenario when starting my react app with react-script start on two instances.

For one instance I see the error boundary overlay screen while for other instance I don't see the error boundary screen whenever there occurs any runtime exception, though both instance have the same code base and started with react-scripts start. I am starting both instances with react-scripts start, because I want both of them to run development mode.

I set NODE_ENV=development and verified in app.js that process.env.NODE_ENV is begin set as development. Still I don't see the error overlay screen for one of the instance when runtime exception. This error is clearly logged in the console. But for one instance I get the error overlay screen while not for the other instance.

As per Tyro below in his answer, it seems that checking NODE_ENV will tell if the app is running in prod or dev mode. But, in my case though NODE_ENV is checked to be set as development, but the behavior is like production mode since, error overlay is not appearing with runtime exception, though gets logged to console.

Question:

1) Checking for NODE_ENV tells about the variable value but how we can know if the app is really running in the environment dictated by NODE_ENV ?

2) In my case, the app is behaving as in production mode even though the value of NODE_ENV checked is 'development' since the error boundary overlay screen is not appearing though error gets logged to console. Is there any other scenario when the error boundar overlay screen won't appear even though in development mode ?

VISHAL DAGA
  • 4,132
  • 10
  • 47
  • 53
  • find a way to check process.env.NODE_ENV, probably expose it to global `window`. ref: https://stackoverflow.com/questions/42458434/how-to-set-build-env-variables-when-running-create-react-app-build-script?answertab=active#tab-top – Tyro Hunter Apr 08 '19 at 08:52
  • But I am setting the NODE_ENV=developement and verified that with echo $NODE_ENV, still the app behaves like in production mode for error boundary, i.e. the overlay error screen doesn't show up. – VISHAL DAGA Apr 08 '19 at 09:11
  • How do you set it? maybe you're doing it wrong. How do you check that variable using `echo $NODE_ENV`? isn't checking suppose to be done within your your app (html/browser)? – Tyro Hunter Apr 08 '19 at 09:16
  • I am running the app in a docker container. I started the app by both `NODE_ENV npm start` as well as setting in the environment NODE_ENV=development and verifying by `echo $NODE_ENV.` Still the error boundary overlay doesn't show up even though I can see the same runtime exception error in the **console logs**. The same error comes as error overlay screen on another instance. Please note the edit to my question. – VISHAL DAGA Apr 08 '19 at 09:20
  • To help you better, kindly provide a code or more information so that we can easily replicate the issue. And also, what kind of `error overlay` are you referring to? Please specify because it could also be a third-party library where the source of issue comes from. Thus, the problem is not from react-scripts – Tyro Hunter Apr 08 '19 at 15:06
  • I am referring to this error overlay: https://reactjs.org/blog/2017/05/18/whats-new-in-create-react-app.html#error-overlay [Update question]. – VISHAL DAGA Apr 08 '19 at 15:17

1 Answers1

-1

I am not sure you can check the NODE_ENV variable used by your CRA app that way. You can't check that in your machine (docker) by issuing echo $NODE_ENV.

  1. To set = Update your react-scripts start command if that is what you use to run your app:

"scripts": { "start": "NODE_ENV=development react-scripts start", ...

  1. To check = within your App.js entry file, simply add console.log('env is:', process.env.NODE_ENV), start the app, open in your browser and show the console, you should see the output of your log.

    or window.MyEnv = process.env.NODE_ENV, open the same console debugger and check the value of window.MyEnv.

Hope this helps!

Tyro Hunter
  • 755
  • 1
  • 8
  • 20
  • Thanks for reply, but unfortunately this doesn't help. I don't know what difference it will make when you do - `NODE_ENV=development npm start` vs putting it inside the definition of `npm start` in `package.json`. Though I tried but no help. I put this `window.MyEnv = process.env.NODE_ENV` in my index.js and when I see in the browser console, I see it is set to 'development'. Still for the same runtime error, there is no error overlay screen! – VISHAL DAGA Apr 08 '19 at 10:00
  • So I think you probably need to update the post or title, because it appears that you are not looking for `How to know an app started with react-scripts start is started in development mode or production mode` – Tyro Hunter Apr 08 '19 at 11:55
  • NODE_ENV sets the environment variable, but that does not mean the app really runs in dev or prod mode. I wanted to know how to know for sure, if the app is in dev or prod mode. – VISHAL DAGA Apr 08 '19 at 13:03
  • I updated the question. If you have the answer to this, please provide. Else, please delete your answer, so that other can know this question is still unanswered. – VISHAL DAGA Apr 08 '19 at 13:18