104

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?

Ryan McDermott
  • 6,127
  • 6
  • 22
  • 23
  • FYI, I was getting this when importing a Component in `react-native` in my `globalSetup` file. Moving this setup from `globalSetup` to `setupFilesAfterEnv` did the trick, since the `jest` test environment is loaded before the files listed in `setupFilesAfterEnv` are run. – Joshua Pinter May 25 '21 at 21:22

4 Answers4

153

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

Community
  • 1
  • 1
Austin
  • 6,241
  • 5
  • 17
  • 12
  • 20
    Also if you are running the app on a physical device using `react-native run-*` – Ricardo Stuven Aug 13 '16 at 17:17
  • 15
    What about when you run in a simulator with `--configuration Release`? Does this still use the React Native packager and set `__DEV__` to `true`? – Marklar Nov 16 '18 at 04:46
  • 1
    @Marklar when running in release configuration, the `__DEV__` is NOT set to true in my experience. – Tope Apr 09 '19 at 12:24
  • `__DEV__` is only about the build configuration. If you want to detect whether the app is running on device or in a simulator, use something like `react-native-device-info`. – Tamlyn May 24 '23 at 10:54
8

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

Raphael Pinel
  • 2,352
  • 24
  • 26
  • 2
    what if I have multiple variant (uat , sit, prod ) and not just simple (debug or release) , I want to know exactly where is that piece of toggle in build gradle turning __DEV__ true or false , i cannot find any resource telling me to do this – elliotching Apr 12 '22 at 09:24
3

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.

Abhishek Kumar
  • 955
  • 11
  • 11
-2

I didn't intend to write an answer, but I'm not able to comment (<50 pts). To toggle __DEV__, you can set it before your command, e.g.:

__DEV__=true expo start (should already be set to true though)

or

__DEV__=false react-native run-ios (should be set to true by default, when running this command without setting manually)