切换分支后执行react-native run-android遇Metro Bundler内部错误求助
Hey there, I’ve dealt with this exact headache before when switching branches in React Native projects—total buzzkill, right? Let’s go through the most reliable fixes that got me back to building:
Wipe all caches (the most common fix)
Corrupted cache files are almost always the culprit after branch switches. Run these commands in sequence:# Reset Metro's cache completely npx react-native start --reset-cache # Clean Android's build artifacts cd android && ./gradlew clean cd ..Then try running
npx react-native run-androidagain.Fix dependency mismatches
Branches often have different dependency versions inpackage.json. Make sure you’re using the right set for your new branch:# Delete old dependencies and lock files rm -rf node_modules rm package-lock.json yarn.lock # remove whichever lock file your project uses # Reinstall dependencies npm install # or yarn install, depending on your package managerDouble-check that your installed versions match what’s listed in the branch’s
package.json—sometimes a branch requires a specific React Native or Metro version that wasn’t installed when you switched.Check Metro/Babel config changes
If the new branch modifiedmetro.config.jsorbabel.config.js, there might be syntax errors or invalid settings. Pop open those files and look for typos, missing commas, or incorrect plugin setups. A tiny mistake here can break Metro entirely.Kill stuck Metro processes
Sometimes Metro doesn’t shut down cleanly when you switch branches, leading to conflicts. Kill any lingering Metro instances:# For macOS/Linux lsof -ti:8081 | xargs kill -9 # For Windows taskkill /F /IM node.exeThen restart the bundler fresh with
npx react-native start.Dig into the terminal logs
The error message says to check your terminal output—don’t skip this! The full logs will usually tell you exactly what’s broken, whether it’s a missing file, a syntax error in your code, or an Android SDK mismatch.
If none of these steps work, sharing your full package.json content and the complete terminal error log would help pinpoint the issue more precisely.
内容的提问来源于stack exchange,提问作者SameerShaik

