React Native新建应用无法运行:连接开发服务器失败问题求助
Hey there, let's break down why your second React Native app is hitting this error even though your first one works flawlessly and ADB recognizes your emulator. Even if you followed the exact same steps, there are a few subtle gotchas that could be tripping you up. Here's what to check:
1. Metro Cache Conflicts
Your first app's Metro bundler might have left behind cached data that's interfering with the new project. Metro sometimes reuses global cache resources, which can cause unexpected behavior across different projects.
Fix:
- Close all React Native-related terminal windows first.
- Start the Metro bundler with a full cache reset:
npx react-native start --reset-cache - In a separate terminal, run your second app again:
npx react-native run-android
2. Port Occupancy Issues
The default Metro port (8081) might still be held by a leftover process from your first app—even if you think you closed everything. When your second app tries to start Metro, it can't grab the port, leading to the connection error.
Fix:
- Check which process is using port 8081:
- Windows:
netstat -ano | findstr :8081 - macOS/Linux:
lsof -i :8081
- Windows:
- Kill the process using the PID from the output:
- Windows:
taskkill /F /PID <your-pid-number> - macOS/Linux:
kill -9 <your-pid-number>
- Windows:
- Alternatively, start Metro on a different port:
Then shake your emulator to open the developer menu, select Change Bundle Location, and enter:npx react-native start --port 8082http://localhost:8082/index.bundle?platform=android&dev=true&minify=false
3. New Project Configuration Glitches
Even with identical setup steps, auto-generated config files in the new project might have minor mismatches (like Gradle/SDK version conflicts) or your project path could have problematic characters.
Fix:
- Check project path: Make sure your second app's folder path has no Chinese characters, spaces, or special symbols (e.g., avoid paths like
C:\我的项目\RNApp2—useC:\RNProjects\App2instead). - Sync Gradle properly: Open the
androidfolder of your second app in Android Studio, click Sync Project with Gradle Files (the elephant icon), and wait for the sync to finish. Then run the app again via command line. - Verify Gradle memory settings: Open
android/gradle.propertiesand ensure theorg.gradle.jvmargsline has enough memory allocated, e.g.:org.gradle.jvmargs=-Xmx2048m -XX:MaxMetaspaceSize=512m
4. Emulator Network Access
ADB recognizing the emulator doesn't guarantee it can reach your local Metro server. Network misconfigurations in the emulator can block the connection.
Fix:
- Shake your emulator to open the developer menu, go to Dev Settings > Debug server host & port for device.
- Enter your computer's local IP address plus the Metro port (e.g.,
192.168.1.105:8081). You can find your local IP usingipconfig(Windows) orifconfig(macOS/Linux). - Restart the emulator and make sure both your computer and emulator are on the same local network.
Try these steps in order—most likely one of them will get your second app up and running smoothly.
内容的提问来源于stack exchange,提问作者KomentaS

