当“react-native run-ios”时,端口8081已被使用

4

是的,我已经阅读了这篇文章,但仍然不知道如何使它工作。

React Native - 端口8081已被使用,打包器要么没有运行,要么没有正确运行。Command /bin/sh失败,退出代码为2

最初我是这样做的:

1. react-native init Hello
2. react-native run-ios

然后出现了“端口8081已经被占用”的错误提示。

我已经阅读了React文档,https://facebook.github.io/react-native/docs/troubleshooting.html

1. cannot kill the process using 8081, it keeps on coming back,
   and i don't want to kill it
2. react-native start --port=8088
3. update node_modules/react-
native/React/React.xcodeproj/project.pbxproj file. Did it.

我应该在哪一步运行"react-native start --port=8088"呢?

顺便说一下,我是个普通工程师,但如果我在两个小时内无法运行第一个React Native样例,我就看不出它如何能够成功,这真的很烦人。

3个回答

9
在RN 0.55中,您可以通过以下方式解决此问题:

react-native start --port=1234

然后在另一个窗口中执行:

react-native run-ios --port 1234

版本信息:
✗ react-native --version
react-native-cli: 2.0.1
react-native: 0.55.4

2
实际上,不需要运行 react-native start --port=1234 命令。只需运行 react-native run-ios --port 1234 即可。 - Bagusflyer

2

在Mac OS上运行React Native应用程序时,由于另一个服务或应用程序使用了端口8081,您需要进行以下操作:

对于iOS模拟器

  1. 打开Xcode并导航到Pods -> Development Pods -> React-Core -> Default -> Base -> RCTDefines。
  2. 更改 #define RCT_METRO_PORT 8081 => #define RCT_METRO_PORT 8088 或所需的端口号(确保在该文件中更改所有出现次数)。
  3. 从项目文件夹中运行 npx react-native run-ios --port 8088(或所需的端口号,必须与RCTDefines文件中的端口号匹配)。

对于Android

  1. 只需从项目文件夹中运行 npx react-native run-android --port=8088(或所需的端口号)。

1
在使用yarn管理的项目中,在package.json文件中。
 "scripts": {
      ...
    "ios": "npx react-native run-ios --port 19001 && npx react-native start --port 19001",
    "android": "npx react-native run-android --port 19001 && npx react-native start --port 19001",
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接