React Native DEV and PROD variables

React Native

React Native Problem Overview


How do I know if my React Native app is running in production or development? Is there some sort of way within JavaScript to tell? Is there a global that is passed in?

React Native Solutions


Solution 1 - React Native

You can use the __DEV__ global variable in JavaScript to determine if you're using React Native packager or not. If you are running your app in the iOS Simulator or Android emulator __DEV__ will be set to true.

https://reactnative.dev/docs/javascript-environment

Solution 2 - React Native

When the __DEV__ variable is set to true, it turns on a bunch of useful development warnings. For production, it is recommended to set __DEV__=false.

Solution 3 - React Native

You can use the __DEV__ variable. By default if you run your app with npx react-native run-ios or npx react-native run-android, it will run in Debug mode and __DEV__ will be true. In release mode, __DEV__ will be false. You can use it this way:

const CLOUD_API_BASE_URL = __DEV__ ? 'https://api-dev.yourdomain.com' : 'https://api-prod.yourdomain.com';

You can run the app in Release mode with the terminal: react-native run-android --variant release #android react-native run-ios --configuration Release #ios

Or open the ios folder in XCode, choose Product > Scheme > Edit Schemes xcode edit scheme Select Run in the left menu. For Build Configuration, choose 'Release' and uncheck 'Debug executable'

enter image description here

In Android Studio, similarly, you can set the build variant to release enter image description here

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRyan McDermottView Question on Stackoverflow
Solution 1 - React NativeAustinView Answer on Stackoverflow
Solution 2 - React NativeAbhishek KumarView Answer on Stackoverflow
Solution 3 - React NativeRaphael PinelView Answer on Stackoverflow