ofundefined
ofundefined

Reputation: 3330

Runtime DEV environment variable for React Js (web)

Just like React Native,

When I am testing some web application in development mode I would like to prevent rendering few blocks.

Is there some runtime environment variable like DEV (boolean) in React Js for web?

I wonder if I may be able to do it by setting some cli param like npm start --dev=true?

Upvotes: 0

Views: 2145

Answers (3)

BertC
BertC

Reputation: 2666

In your 'npm start' you could pass environment variables. Put the following in your package.json

 "scripts": {
    "start": "ENV=PRODUCTION react-scripts start",
    "dev": "ENV=DEV react-scripts start"
  }

Then do 'npm start' for production and 'npm run dev' for dev setup.

Upvotes: 0

SakoBu
SakoBu

Reputation: 4011

You can check the env like so:

if (process.env.NODE_ENV === 'development') {
  console.log('dev');
}

Upvotes: 0

yagiro
yagiro

Reputation: 777

If your react project is a create-react-app project then there is a special built-in environment variable called NODE_ENV. You can read it from process.env.NODE_ENV. When you run npm start, it is always equal to 'development', when you run npm test it is always equal to 'test', and when you run npm run build to make a production bundle, it is always equal to 'production'.

Upvotes: 2

Related Questions