xcode React Native - 端口 8081 已在使用,打包程序未运行或未正确运行命令 /bin/sh 失败,退出代码 2

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30216417/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 18:07:38  来源:igfitidea点击:

React Native - Port 8081 already in use, packager is either not running or not running correctly Command /bin/sh failed with exit code 2

xcodereactjs-native

提问by user1454373

I'm trying to get up and running with React Native and I am seeing the message below in Xcode:

我正在尝试启动并运行 React Native,我在 Xcode 中看到以下消息:

Port 8081 already in use, packager is either not running or not running correctly

Command /bin/sh failed with exit code 2

端口 8081 已在使用,打包程序未运行或未正确运行

命令 /bin/sh 失败,退出代码为 2

I went to the React Native troubleshooting pageand tried to kill the port 8081 processes, but I'm still getting the same issue.

我转到React Native 故障排除页面并尝试终止端口 8081 进程,但我仍然遇到相同的问题。

Here is a screenshot of what I am seeing in Xcode:

这是我在 Xcode 中看到的截图:

error screenshots

错误截图

Any help would be fully appreciated.

任何帮助将不胜感激。

采纳答案by jamiewq

With the help of other people's answers. I tried the following steps. It worked for me and hopefully for others. It only works for iOS.Let's say we want to change the 8081 port to 8999 port.

在其他人的答案的帮助下。我尝试了以下步骤。它对我有用,并希望对其他人有用。它仅适用于 iOS。假设我们要将8081 端口更改为 8999 端口

First, Open Xcode.

首先,打开Xcode。

  • Look at Project navigator(left) In [ProjectName]/[ProjectName]/AppDelegate.m:

    Change

    http://localhost:8081/index.ios.bundle?platform=ios&dev=true 
    

    to

    http://localhost:8999/index.ios.bundle?platform=ios&dev=true
    
  • In Project navigator(left) [ProjectName]/ Libraries:

    Click “React.xcodeproj”. On main panel, click “Build Phases” tag.

    Expand “Run Script”, delete it with the cross.

  • In Project navigator(left) [ProjectName] / Libraries / RCTWebSocket.xcodeproj / RCTWebSocketExecutor.m : Search 8081 and replace it with 8999

  • 查看 Project navigator(left) 在 [ProjectName]/[ProjectName]/AppDelegate.m 中:

    改变

    http://localhost:8081/index.ios.bundle?platform=ios&dev=true 
    

    http://localhost:8999/index.ios.bundle?platform=ios&dev=true
    
  • 在项目导航器(左)[项目名称]/库中:

    单击“React.xcodeproj”。在主面板上,单击“构建阶段”标签。

    展开“运行脚本”,用叉号删除它。

  • 在 Project navigator(left) [ProjectName] / Libraries / RCTWebSocket.xcodeproj / RCTWebSocketExecutor.m 中:搜索 8081 并替换为 8999

Second open Finder

第二次打开 Finder

In the project root, open “package.json” :

在项目根目录中,打开“package.json”:

Change the “script” attribute to :

将“脚本”属性更改为:

{...

    "start": "node_modules/react-native/packager/packager.sh --port=8999"
...
}

Then Open terminal

然后打开终端

  • $cd to project root :

    $ npm start

  • $cd 到项目根目录:

    $ npm 开始

Cool! Then

凉爽的!然后

Go back to Xcode and click the play button.

返回Xcode 并单击播放按钮。

Cross your fingers.

交叉你的手指。

Be Patient. There will be a blank in the client.

要有耐心。客户端会有一片空白。

You can see it is doing building on the backend(terminal will log it).

您可以看到它正在后端构建(终端将记录它)。

回答by escouten

If you do lsof -n -i4TCP:8081as recommended in Facebook's troubleshooting pageand get an empty result, try again using sudo lsof -n -i4TCP:8081.

如果您lsof -n -i4TCP:8081按照Facebook 的故障排除页面中的建议进行操作并获得空结果,请再次尝试使用sudo lsof -n -i4TCP:8081.

In my case, it turns out McAfee anti-virus software is running process that listens on that port. Killing that process (I know, I know!) fixed the problem.

就我而言,事实证明 McAfee 防病毒软件正在运行侦听该端口的进程。杀死那个进程(我知道,我知道!)解决了这个问题。

回答by Emile

Try the following steps for those that need to change port 8081 to a different port.

对于需要将端口 8081 更改为其他端口的用户,请尝试以下步骤。

> npm start

> npm start

will launch node_modules/react-native/packager/packager.sh

将启动node_modules/react-native/packager/packager.sh

In there it will merge command line parameters, i.e. --port into the predefined options. i.e. port=8081

在那里它将合并命令行参数,即 --port 到预定义的选项中。即端口=8081

I updated the package.json startoption to include my prefered port, as i was unable to stop existing services using this port.

我更新了 package.jsonstart选项以包含我的首选端口,因为我无法停止使用此端口的现有服务。

{
  "name": "AwesomeProject",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "start": "node_modules/react-native/packager/packager.sh --port=8999"
  },
  "dependencies": {
    "react-native": "^0.12.0"
  }
}

** Note that this may not work for android which is apparently hard coded to 8081 Stack Post Here

** 请注意,这可能不适用于显然硬编码为 8081 Stack Post Here 的android

Building XcodeWhen building Xcode it will still fail as it tries to run a script to launch node. You will either need to remove this script from the build process or update it to the new port.

构建 Xcode构建 Xcode 时,它​​仍然会失败,因为它尝试运行脚本来启动节点。您需要从构建过程中删除此脚本或将其更新到新端口。

Under librariesselect React.xcodeproj. In the main screen select Build Phases. You will see Run Script.

libraries选择下React.xcodeproj。在主屏幕中选择Build Phases。你会看到Run Script

Either remove this entry, having called npm startyourself, or edit the port.

删除此条目(已呼叫npm start自己)或编辑端口。

if nc -w 5 -z localhost 8999 ; then
  if ! curl -s "http://localhost:8999/status" | grep -q "packager-status:running" ; then
    echo "Port 8999 already in use, packager is either not running or not running correctly"
    exit 2
  fi
else
  open $SRCROOT/../packager/launchPackager.command || echo "Can't start packager automatically"
fi

DebuggingSeems 8081 is all over the shop. Need to additionally updated the RCTWebSocketExecutor.m under xcode-project: Libraries/RCTWebSocket.xcodeproj

调试似乎 8081 到处都是。需要另外更新xcode-project下的RCTWebSocketExecutor.m:Libraries/RCTWebSocket.xcodeproj

- (instancetype)init
{
  return [self initWithURL:[RCTConvert NSURL:@"http://localhost:8999/debugger-proxy"]];
}

** Launching packager from iOS ** If launching only from iOS then you additionally need to edit launchPackager.command to add in the appropriate port as this file is used by Xcode to run the javascript.

** 从 iOS 启动打包程序 ** 如果仅从 iOS 启动,那么您还需要编辑 launchPackager.command 以添加适当的端口,因为 Xcode 使用此文件来运行 javascript。

$THIS_DIR/packager.sh  --port=8999

回答by LeOn - Han Li

Had the same problem!

有同样的问题!

For Android I can use adb to redirect port, but in iOS, cannot figure our a way to run React Native in a custom port. Have to take over the 8081 port from McAfee.

对于 Android,我可以使用adb 重定向 port,但在 iOS 中,无法找到在自定义端口中运行 React Native 的方法。必须从 McAfee 接管 8081 端口。

For those Mac user who cannot kill the McAfee process/service directly, you can unload it via launchctl(macOS), then you can run the packager server on the default 8081 port.

对于无法直接杀死 McAfee 进程/服务的 Mac 用户,可以通过launchctl(macOS)卸载,然后在默认的 8081 端口上运行打包服务器。

cd /Library/LaunchDaemons
sudo launchctl unload com.mcafee.agent.macmn.plist

Also wrote a memo to explain the launchctl detail and MacOS boot flow.

还写了一份备忘录来解释launchctl细节和MacOS启动流程

回答by Arshid KV

Easy method:Try with the following code

简单方法:试试下面的代码

kill -9 $(lsof -t -i:8081)

回答by Sarosh Arunkumar

run the following command:

运行以下命令:

react-native start --port=8088

react-native start --port=8088

I had the same issue with McAfee running on 8081. This worked for me.

我在 8081 上运行 McAfee 时遇到了同样的问题。这对我有用。

https://facebook.github.io/react-native/docs/troubleshooting.html

https://facebook.github.io/react-native/docs/troubleshooting.html

回答by user1454373

I figured out the issue: for some reason I didn't kill the process on port 8081and it was causing Xcode to fail.

我发现了这个问题:出于某种原因,我没有终止进程port 8081,而是导致 Xcode 失败。

Solution:

解决方案:

  1. Kill the process on port 8081.
  2. Clean Xcode: Xcode Menu > Product > Clean.
  3. Reopen Xcode.
  1. 杀死进程port 8081
  2. 清洁 Xcode:Xcode Menu > Product > Clean.
  3. 重新打开 Xcode。

Resource:

资源:

React-Native Troubleshooting

React-Native 故障排除

回答by Vijay

I had the same issue running react-native run-ios ----port=8088worked for me, did not try android yet

我有同样的问题运行react-native run-ios ----port=8088对我有用,还没有尝试 android

回答by felixwcf

My problem was the Wifi DNS setup..

我的问题是 Wifi DNS 设置..

i. Go to Settings-> Network.

一世。转到设置->网络

ii. Select the connected Wifi, click Advance.

ii. 选择连接的Wifi,点击Advance

iii. Select DNS, you might have set your DNS server to 8.8.8.8(God knows your intention ;) ). Delete it and set it to the default 192.168.1.1. Click OK.

三、选择DNS,您可能已将 DNS 服务器设置为8.8.8.8(上帝知道您的意图;))。删除它并将其设置为默认192.168.1.1。单击确定。

iv. Run the react-native run-iosin terminal and it works.

四、在终端中运行react-native run-ios并且它可以工作。

回答by Gualtiero Frigerio

I just had the same problem and found out Docker was using port 8081.

我刚遇到同样的问题,发现 Docker 正在使用端口 8081。

Therefore if you need to debug stuff with React Native, you need to stop docker, if it is using the same port.

因此,如果您需要使用 React Native 进行调试,则需要停止 docker,如果它使用相同的端口。