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

后端 未结 12 1193
眼角桃花
眼角桃花 2020-12-13 11:01

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

Port 8081 already in use, packager is either not running

相关标签:
12条回答
  • 2020-12-13 11:28

    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.

    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.

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

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

    0 讨论(0)
  • 2020-12-13 11:29

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

    Solution:

    1. Kill the process on port 8081.
    2. Clean Xcode: Xcode Menu > Product > Clean.
    3. Reopen Xcode.

    Resource:

    React-Native Troubleshooting

    0 讨论(0)
  • 2020-12-13 11:29

    My problem was the Wifi DNS setup..

    i. Go to Settings-> Network.

    ii. Select the connected Wifi, click 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.

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

    0 讨论(0)
  • 2020-12-13 11:32

    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.

    First, Open 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

    Second open Finder

    In the project root, open “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

    Cool! Then

    Go back to Xcode and click the play button.

    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).

    0 讨论(0)
  • 2020-12-13 11:33

    The following steps worked for me for running react-native code on iOS and XCode 10.1

    1. In the project root folder, go to ios folder and double click on {projectName}.xcodeproject file. This will open XCode.
    2. In XCode, select File >> Project Settings.
    3. This will open the settings page, in the same page change "Per-User Project Settings:" >> "Build System" from 'User Shared Setting' to 'Legacy Build System'
    4. Save your changes.
    5. Then npm install @babel/runtime --save-dev
    6. Then run the project using the command react-native run-ios --port=8088. This will run the packager in 8088 port.
    0 讨论(0)
  • 2020-12-13 11:35

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

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

    0 讨论(0)
提交回复
热议问题