0

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?

1
  • react.js runs client-side, so it doesn't have automatic access to vars in your server or shell env. are you using webpack, @Magalhães Lage ? Commented Feb 4, 2019 at 21:53

3 Answers 3

2

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'.

Sign up to request clarification or add additional context in comments.

Comments

0

You can check the env like so:

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.