Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set NODE_ENV from package.json for react

I am trying to target multiple environments from local while executing react app.

1. Development
2. Staging
3. Production

I am also trying to test for offline mode in any of the environments. So, the scripts what I have configured is as follows:

    "staging-server": "nodemon server.js --environment=staging",
    "staging": "concurrently -k  \"npm:staging-server\" \"NODE_ENV='staging' PORT=3003 react-scripts start\"",
    "prod": "npm run build && forever server.js --environment=production"

I am able to fetch environment arg using args inside my express, but my local ui app is still showing development only when I console for process.env.NODE_ENV. I am also trying to set NODE_ENV with same line for staging, but still no luck. PORT setting is working but, the app is running in 3000 and 3003 both ports. How to get rid off this and help me understand the staging configuration as well by providing useful links or code is fine

like image 821
Mithun Shreevatsa Avatar asked Jul 31 '19 07:07

Mithun Shreevatsa


People also ask

Does react set NODE_ENV?

NODE_ENV . When you are using Create React App, the react-scripts will set the value of NODE_ENV to development when npm start is executed and to production when you run npm run build .

Does npm run build set NODE_ENV?

The value of NODE_ENV is set automatically to development (when using npm start ), test (when using npm test ) or production (when using npm build ).


2 Answers

As per the docs, we cannot override NODE_ENV, but there is a room to create our own custom variables starting with REACT_APP_. So i configured to look as below:

Reference: https://facebook.github.io/create-react-app/docs/adding-custom-environment-variables

"staging": "concurrently -k  \"npm:staging-server\" \"cross-env REACT_APP_ENVIRONMENT='staging' PORT=3003 react-scripts start\"",

And inside my UI application, I can fetch its value by consoling it like this: console.log('REACT_APP_ENVIRONMENT => ', process.env.REACT_APP_ENVIRONMENT);

like image 186
Mithun Shreevatsa Avatar answered Oct 03 '22 07:10

Mithun Shreevatsa


I build the build with REACT_APP_STAGE and use it in my application as process.env.REACT_APP_STAGE.

"scripts": {
    "analyze": "source-map-explorer 'build/static/js/*.js'",
    "build-css": "node-sass-chokidar --include-path ./src --include-path ./node_modules src/ -o src/",
    "watch-css": "npm run build-css && node-sass-chokidar --include-path ./src --include-path ./node_modules src/ -o src/ --watch --recursive",
    "start-js": "react-scripts start",
    "start": "REACT_APP_STAGE=local npm-run-all -p watch-css start-js",
    "build": "npm run build-css && react-scripts build",
    "build-dev": "REACT_APP_STAGE=dev react-scripts build",
    "build-prod": "REACT_APP_STAGE=prod react-scripts build",
    "build-qa": "REACT_APP_STAGE=qa react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject"
  },
like image 31
Aman Gupta Avatar answered Oct 03 '22 08:10

Aman Gupta