6

I want to deploy a React app (made with Create React App) + a Node server with Heroku,

I did it, but my app can't fetch data from the server,

In production, my process.env.NODE_ENV is equal to "development" which causes a lot of wrong stuff in my code,

Do you know what can put process.env.NODE_ENV always at "development"? At the build, this environment variable is supposed to switch to "production", no?

Victor Baranov
  • 153
  • 2
  • 8
VersifiXion
  • 2,152
  • 5
  • 22
  • 40

3 Answers3

1

Your package.json add this.

  "scripts": {
    "start": "export NODE_ENV=development; {your start code}",
seunggabi
  • 1,699
  • 12
  • 12
0

Your env variables can be set per environment, in this case in Heroku: https://devcenter.heroku.com/articles/config-vars#using-the-heroku-dashboard

If you want to make sure build always runs with the same NODE_ENV, you can follow @seunggabi 's answer. I'd also use cross-env to make it work cross-platform in such case. Per-process variable can be forced on heroku-postbuild task (after &&).

Tymek
  • 3,000
  • 1
  • 25
  • 46
  • hi, I also have the script heroku postbuild in my package json ("heroku-postbuild": "npm install && npm run build"), but it looks like the build is not done because my node_env variable is still at development... – VersifiXion Dec 30 '19 at 19:09
  • 1
    My suggestion was to try `heroku-postbuild` as `npm install && cross-env NODE_ENV=production npm run build` (after adding `cross-env` to dev-dependencies) – Tymek Dec 30 '19 at 19:27
-1

You can take control of your Environment with env-cmd. They make easy to switch between your local development, testing, staging, UAT or production.

You can refer to this article. This is was very helpful for me

Mayur Sonawane
  • 109
  • 1
  • 8
  • 1
    thank you for the resource, I read the article, but if I'm not wrong, I still don't understand why my process.env.NODE_ENV is at "development" in production :'( – VersifiXion Dec 31 '19 at 11:11
  • @Versifiction you tried `npm run build` or command that creates optimized production bundle? – Mayur Sonawane Jan 06 '20 at 06:52
  • yes the script is done every time I deploy but the env variable it still at development, but don't worry I did it with another solution – VersifiXion Jan 06 '20 at 11:20