Linux环境下React Native npm start报错(端口8081被占用)及相关问题
Hey there, let's cut straight to the core of your issue: that Error: listen EADDRINUSE :::8081 line makes it clear the default Metro Bundler port (8081) is already occupied by another process. That's why your npm start fails, and react-native run-android gets stuck loading—your app can't connect to the bundler it needs to load code.
You've already tried some general React Native troubleshooting steps (cleaning node_modules, reinstalling watchman), but let's target the port conflict directly with these solutions:
1. Identify and Kill the Process Using Port 8081
First, find out which program is hogging the port:
- Run this command to list processes using port 8081:
Look for thelsof -i :8081PIDcolumn (the numerical ID of the process using the port). - If
lsofisn't installed on your system, use this alternative command:netstat -tulpn | grep 8081 - Once you have the PID, terminate the process with:
Replacesudo kill -9 <PID><PID>with the actual number you found (e.g.,sudo kill -9 1234).
2. Start Metro on a Different Port (If You Need to Keep 8081 in Use)
If you can't kill the process using 8081, just tell Metro to use an alternative port:
- Start the bundler with a custom port:
npm start -- --port 8082 - Then run your Android app pointing to the new port:
react-native run-android --port 8082 - To make this permanent, add a
metro.config.jsfile in your project root with:module.exports = { server: { port: 8082, }, };
3. Extra Troubleshooting to Cover All Bases
Since you already tried reinstalling node_modules and watchman, add these checks to ensure everything is working:
- Reset Metro's cache (this often fixes stuck loading issues even after resolving port conflicts):
npx react-native start --reset-cache - After reinstalling watchman, close your terminal and open a new one to ensure the changes take effect.
- Verify watchman is installed correctly with:
watchman version
Quick Note on Your Project Setup
Your package.json looks completely standard for React Native 0.59.5—no configuration issues stand out here. The problem is 100% related to the port conflict, not your project's dependencies or scripts.
Give these steps a try, and your app should load properly once Metro can start up successfully!
内容的提问来源于stack exchange,提问作者mad_greasemonkey

