执行react-native run-android命令报错500:安卓设备/模拟器中命令终止
Hey there, let's break down why you're hitting that 500 error when running react-native run-android on both physical Android devices and emulators. This is a super common gotcha, so don't stress—here are the most likely culprits and fixes:
1. Metro Server Isn't Running (or Crashed Silently)
Sometimes the Metro bundler fails to spin up automatically, or crashes in the background without you noticing. Try this:- Kill any existing Metro processes (close the terminal tab it's running in, or use
killall nodeon macOS/Linux,taskkill /F /IM node.exeon Windows). - Manually start Metro with
npx react-native start --reset-cache—the--reset-cacheflag is key here, since stale cached files are a frequent cause of this error. - Once Metro is up and running smoothly, open a fresh terminal and run
react-native run-androidagain.
- Kill any existing Metro processes (close the terminal tab it's running in, or use
2. Port Conflicts
Metro uses port 8081 by default, and if another app is hogging that port, the server throws a 500 error.- Check if port 8081 is occupied: run
lsof -i :8081(macOS/Linux) ornetstat -ano | findstr :8081(Windows). If you see a process using it, terminate it. - Alternatively, run Metro on a different port with
npx react-native start --port 8082, then match it withreact-native run-android --port 8082.
- Check if port 8081 is occupied: run
3. Network Mismatch Between Device/Emulator and Dev Machine
Both physical devices and emulators need to reach your Metro server, which requires proper network setup.- For physical devices: Ensure your phone is on the same Wi-Fi as your computer. If using USB debugging, run
adb reverse tcp:8081 tcp:8081to bypass network routing issues. - For emulators: Sometimes emulators struggle to resolve your computer's IP. Open the developer menu (shake the emulator or press
Ctrl+M/Cmd+M), select "Change Bundle Location", and enter your computer's local IP plus:8081(e.g.,192.168.1.100:8081).
- For physical devices: Ensure your phone is on the same Wi-Fi as your computer. If using USB debugging, run
4. Corrupted Dependencies or Build Artifacts
Broken node modules or stale Android build files can trigger unexpected server errors.- Delete the
node_modulesfolder and reinstall dependencies:rm -rf node_modules && npm install(oryarn installif you use Yarn). - Clean the Android build folder:
cd android && ./gradlew clean && cd .., then re-runreact-native run-android.
- Delete the
5. Outdated React Native or Dependencies
Older versions of React Native or related packages might have bugs that cause this 500 error.- Verify if your React Native version is up to date (check the latest stable release notes for guidance). If not, follow the official upgrade steps tailored to your project.
- Run
npm auditto check for vulnerable dependencies and update them as needed.
If none of these fix the issue, take a close look at the full Metro server logs—sometimes the 500 error hides a more specific message (like a syntax error in your code) that's printed in the Metro terminal tab. That can give you a clearer clue about what's going wrong.
内容的提问来源于stack exchange,提问作者Kashifweb

