Ubuntu系统Expo Client无法连接Packager问题求助
Hey there, let's work through this frustrating Expo connection problem you're facing on Ubuntu. It makes total sense to suspect your MacBook run is linked—cached settings or network conflicts can linger even after restarting everything. Let's break down actionable fixes step by step:
1. Check if Port 19001 is Blocked or Occupied
First, let's confirm if something's hogging the packager port Expo needs. Run this in your terminal:
sudo lsof -i :19001
If you see any processes listed, kill them with:
sudo kill -9 <PID>
Replace <PID> with the process ID from the output. Then restart your Expo server with npm start and try connecting again.
2. Wipe All Cached Data (Likely the Culprit!)
Cached files from your MacBook project run are probably messing with your Ubuntu setup. Let's clear everything out:
- Stop any running Expo servers first.
- Run these commands one by one:
expo r -c # Clears Expo's project-specific cache rm -rf node_modules # Deletes old dependencies npm install # Fresh install of packages rm -rf ~/.expo # Wipes global Expo settings/cache
This resets your project and Expo's global state to a clean slate.
3. Fix Port Configuration (Your Earlier Attempts Were Almost Right!)
react-native-scripts handles port settings differently than plain Expo CLI. Try these correct methods:
Option A: Update Package.json Scripts Properly
Edit your package.json start script to explicitly set both the main server and packager ports:
"scripts": { "start": "PORT=19000 PACKAGER_PORT=19001 react-native-scripts start" }
Expo uses 19000 for the main server, 19001 for the packager, so setting both ensures they're aligned.
Option B: Switch to Expo CLI Directly
If react-native-scripts is being stubborn, use Expo CLI instead—it gives more control. First install it globally:
npm install -g expo-cli
Then start your project with explicit port commands:
expo start --port 19000 --packager-port 19001
This bypasses any quirks with react-native-scripts.
4. Verify Network Settings & Firewall
Double-check that your phone and Ubuntu machine are on the same Wi-Fi network. Then confirm your Ubuntu's correct local IP:
ip addr show
Look for the inet line under your active network adapter (e.g., 192.168.1.x). Manually enter exp://<your-ip>:19000 in Expo Client—don't rely on the QR code if it's picking up the wrong IP.
Also, make sure Ubuntu's firewall isn't blocking Expo ports:
sudo ufw allow 19000/tcp sudo ufw allow 19001/tcp sudo ufw allow 19000/udp sudo ufw allow 19001/udp
Expo uses both TCP and UDP for communication, so opening both is key.
5. Reset Expo Client on Your Phone
Your phone's Expo app might have cached old connection data from your MacBook. Try:
- Closing the app completely (swipe it away from recent apps).
- Clearing the app's cache (Android: Settings > Apps > Expo > Storage > Clear Cache; iOS: Delete and reinstall the app if cache clearing isn't an option).
- Reopen the app and try connecting again.
6. Resolve Network Conflicts from Your MacBook
Even with your MacBook off, network leases or DNS entries can linger. Try:
- Restarting your router again (do this after clearing all caches above).
- Renewing your Ubuntu machine's IP address:
sudo dhclient -r sudo dhclient
This gets a fresh IP from your router, fixing any leftover conflicts.
7. Test with a Fresh Expo Project
If none of the above works, create a new test project to rule out issues with your existing code:
expo init test-project cd test-project npm start
If this test project connects fine, copy your source code into the new project and reinstall dependencies—your original project's config might be corrupted.
Hope one of these fixes gets your Expo connection back up and running! 🚀
内容的提问来源于stack exchange,提问作者awwester

