You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Windows系统下React Native中端口8081已被占用错误的解决方法

错误:listen eaddrinuse: 地址已被占用 :::8081
错误:listen EADDRINUSE: 地址已被占用 :::8081

at Server.setupListenHandle [as _listen2] (node:net:1372:16)  
at listenInCluster (node:net:1420:12)  
at Server.listen (node:net:1508:7)  
at D:\RND\Mobile\node_modules\metro\src\index.js:164:16  
at new Promise (<anonymous>)  
at Object.exports.runServer (D:\RND\Mobile\node_modules\metro\src\index.js:163:10)  
at async Object.runServer [as func] (D:\RND\Mobile\node_modules\@react-native-community\cli-plugin-metro\build\commands\start\runServer.js:121:26)
at async Command.handleAction (D:\RND\Mobile\node_modules\@react-native-community\cli\build\index.js:192:9)

提示:使用--verbose标志运行CLI以获取更多详细信息。

Windows下React Native 8081端口被占用的解决方法

方法一:结束占用端口的进程

  • 打开命令提示符(CMD),执行命令:
    netstat -ano | findstr :8081
    输出结果最后一列的数字即为占用8081端口的进程PID
  • 执行命令强制结束该进程:
    taskkill /PID <替换为找到的PID> /F

方法二:临时切换Metro服务端口

  • 启动项目时指定新端口(比如8082):
    npx react-native start --port 8082
  • 在模拟器/真机中打开开发者菜单(Android按Ctrl+M,iOS按Cmd+D),选择「Change Bundle Location」,输入对应端口的bundle地址:
    Android:http://localhost:8082/index.bundle?platform=android&dev=true&minify=false
    iOS:http://localhost:8082/index.bundle?platform=ios&dev=true&minify=false

方法三:永久修改默认端口

1. 修改Metro配置

在项目根目录的metro.config.js中添加端口配置:

module.exports = {
  server: {
    port: 8082, // 替换为你想要的端口
  },
};

2. 修改Android端配置

打开android/app/src/main/java/com/[你的项目名称]/MainApplication.java,找到ReactNativeHost的实现,添加端口重写方法:

@Override
protected int getPort() {
  return 8082; // 与Metro配置的端口一致
}

3. 修改iOS端配置

打开ios/[你的项目名称]/AppDelegate.m,找到jsCodeLocation的赋值代码,将端口改为8082:

jsCodeLocation = [NSURL URLWithString:@"http://localhost:8082/index.bundle?platform=ios&dev=true&minify=false"];

内容的提问来源于stack exchange,提问作者Nagaraj sk

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 11:18:38