Search code examples
androidreactjsreact-nativemetro-bundler

React-Native Run-Android Doesn't recognize Open Metro Server


Issue: This broke with no changes between days. Even though I have metro running npx react-native start when I run npx react-native run-android it starts it's own metro server and does not allow me to do "fast refresh" it basically installs the debug build of the app onto the device. I need it to fast refresh for development purposes. Already tried the steps on issue: Unable to load script from assets index.android.bundle on windows

To test and debug my android build for a react-native project I had an emulator running in one Powershell window, would start a metro server in a second Powershell window using npx react-native start then run the app in a third powershell window using npx react-native run-android it would go through the build and install the app, then it would "pass" the app to the react-native metro server then that would install it to the emulator. This would allow me to make changes to the code and "fast refresh" the application in the emulator/phone and logs got outputted to the Metro Server.

Versions (executed at project root folder)

npx react-native --version : 6.0.0

npx --version : 7.18.1

Setup:

First Powershell window: emulator -avd Galaxy_Nexus_Android9 -no-snapshot

Second Powershell window: Make sure abd device is running

adb devices

result

List of devices attached
emulator-5554   device

run npx react-native start

result: Metro JS Server Start

Third Powershell Window run: npx react-native run-android

result: Run Android

I boxed the line where it launches its own JS server instead of using the one that is open.

The app opens fine on the emulator (I initialized a new app to make sure there was nothing wrong with the app) App Running Android

But on the Metro window, it did not install the app and when I try to reload it says it cannot detect any running apps Metro Restart I've already tried the resolution here to run it on a different port with no success No apps connected. Sending "reload" to all React Native apps failed. Make sure your app is running in the simulator or on a phone connected via USB


Solution

  • Solution: Run the start metro server and run-android commands on a different port. I suspect McAfee was using the default port 8081.

    Open admin CMD/Powershell and run the following:

    Reverse the port that the adb device will listen on: adb reverse tcp:8081 tcp:8090

    Start the Metro server on the same port: npx react-native start --port 8090

    Open another admin CMD/Powershell window and run:

    Start the android build/run on the same port: npx react-native run-android --port=8090

    The last command should also start the emulator automatically.

    If the metro server errors out after you do the run-android command, just run the npx react-native start --port 8090 again in the same window.