How to know on Client in react is env prod or staging

Viewed 1091

How to know in react app on client, does current env is prod or staging?

On Backend / node js we have process.env.STAGE_NAME

But that now work on client.

This is package.json.

How I get that info on Client?

"scripts": {
    "postinstall": "cd ./localDb && sls dynamodb install && cd ../",
    "buildServer": "tsc -b ./server/tsconfig.json",
    "buildClient": "next build ./client",
    "build": "npm run buildServer && npm run buildClient",
    "build:dev": "npm run buildServer && NODE_ENV=development  npm run buildClient",
    "dev": "node --inspect server/build/index.js",
    "start": "NODE_ENV=production node server/build/index.js",
    "testClient": "nyc --reporter=html --reporter=text mocha --timeout 15000 --reporter mochawesome --exit",
    "testServer": "nyc --reporter=html --reporter=text mocha --timeout 15000 --reporter mochawesome --exit server/build/tests/*.js",
    "lintServer": "tslint 'server/**/*.ts' --quiet --project server/tsconfig.json",
    "lintClient": "eslint '**/*.{js,jsx}' --quiet",
    "lint": "npm run lintClient && npm run lintServer",
    "format": "prettier --write '**/*.{js,jsx,css,scss,ts}'",
    "test": "npm run testClient && NODE_ENV=testing npm run testServer",
    "local:db": "cd ./localDb && sls dynamodb start && dynamodb-admin"
  },
2 Answers

You can do this,

"scripts": {
    "build": "npm run buildServer && npm run buildClient",
    "build:dev": "npm run buildServer && NODE_ENV=development  npm run buildClient",
    "build:stage": "npm run buildServer && NODE_ENV=staging npm run buildClient",
  },

Now you can know the environment from process.env.NODE_ENV.

If you have created the app using create-react-app I would recommend using .env file. Check the link for further read, Link

It depends on how did you run the react app and what did you define it in package.json

on depend on your package.json and NODE_ENV= parameter:

Start app for test env : npm test

Start app for prod env : npm start

Related