2

So I'm getting started with React Native. Honestly working with XCode has been one of the most miserable development experiences I've experienced in my life. The simulator literally takes 20+ minutes to boot up (is this normal??) and when it does I can't launch the app because it's on port 8081 which apparently something else is running on so it just crashes. So now I have to change it and probably wait another 30 minutes for the simulator to boot up.

My question is, what is the best way to change the port number in a React Native iOS app. I've seen references to the appDelegate.m file but it seems that it's not allowing you to change it in this file anymore because I see no reference to localhost:8081 anywhere in it.

Any help would be appreciated. I also have tried react-native start --port 9988 this starts a terminal session and looks like it's working but doesn't launch xcode, the simulator or anything. When I launch Xcode and then run the app from here it launches another terminal session pointing to port 8081 pretty much just undoing what I did.

Been basically sitting and waiting on xcode for the last 5 hours. Super frustrating. Just wanna start coding!!

Thanks!

maxwellgover
  • 5,001
  • 8
  • 31
  • 55
  • When I Google `react native change port` there seem to be loads of answers to this exact question, do they not work for you? – Pekka Dec 03 '16 at 21:15
  • (The simulator should not take 20 minutes to load on a more-or-less modern Mac, though. It takes maybe 10-20 secs on my 2011 MBP 15" - albeit with a SSD) – Pekka Dec 03 '16 at 21:15
  • What I find is either referencing android or referencing changing the appDelegate.m file but there is no reference to the port in there so I can't change it. – maxwellgover Dec 03 '16 at 21:17
  • Maybe it's moved elsewhere. That happens frequently with RN. Hang on, I'll check – Pekka Dec 03 '16 at 21:18
  • What I would try is, in XCode: Find > Find in Project..., search for "8081". I have a RN project here from 2 weeks back and there seem to be two relevant lines, one in RCTWebSocketExecutor.m, one in RCTbundleURLProvider.m, and one in RCTDevMenu.m. Not sure which is which but I'd try and change both. Might finding out what is running on port 8081 and changing *that* not be the easier way, though? http://stackoverflow.com/questions/4421633/who-is-listening-on-a-given-tcp-port-on-mac-os-x – Pekka Dec 03 '16 at 21:22
  • What kind of machine are you developing on? – Pekka Dec 03 '16 at 21:22
  • Macbook pro (Early 2011) running OS Sierre and just d/l latest version of xcode. – maxwellgover Dec 03 '16 at 21:24
  • Hmm, that the simulator would take 20 *minutes* to start up sounds really odd. Not sure what to recommend, except make sure you have enough disk space. Either way, I'd try and find out what is occupying port 8081 first and see whether you need that at all – Pekka Dec 03 '16 at 21:28
  • I ran `lsof -i -P` but nothing showing up w/ port 8081 – maxwellgover Dec 03 '16 at 21:31
  • Are you sure that's the problem then? What error message are you getting? – Pekka Dec 03 '16 at 21:45
  • I'm sure it's the problem because I've ran into it before. I changed the port number and it runs fine. Just not sure where to change it now that RN has moved it. – maxwellgover Dec 03 '16 at 21:49
  • I just ran `ps aux | grep 8081` and got the following output `user 7026 100.0 0.0 2450212 852 s000 R+ 4:44PM 0:00.00 grep 8081` not sure what this means. – maxwellgover Dec 03 '16 at 21:50
  • `Just not sure where to change it now that RN has moved it.` Searching for `8081` in the XCode project should give you all the relevant lines you need to change. – Pekka Dec 03 '16 at 21:51
  • I'd try `sudo lsof -iTCP -sTCP:LISTEN -n -P` to get a clear-name list of all the services listening in – Pekka Dec 03 '16 at 21:52
  • `cupsd 233 root 10u IPv6 0x87bfcd779ba0d747 0t0 TCP *:631 (LISTEN) cupsd 233 root 11u IPv4 0x87bfcd779ae520a7 0t0 TCP *:631 (LISTEN) Dropbox 709 user 109u IPv6 0x87bfcd779ba0dc87 0t0 TCP *:17500 (LISTEN) Dropbox 709 user 111u IPv4 0x87bfcd779606999f 0t0 TCP *:17500 (LISTEN) Dropbox 709 user 135u IPv4 0x87bfcd77986d699f 0t0 TCP 127.0.0.1:17600 (LISTEN) Dropbox 709 user 141u IPv4 0x87bfcd77986d57af 0t0 TCP 127.0.0.1:17603 (LISTEN)` This is the only thing that comes up so I don't know I'm at a loss right now. – maxwellgover Dec 03 '16 at 22:11

2 Answers2

4

Had the same problem, I found that the React library takes a user defined setting to change the default port : RCT_METRO_PORT in : -> Libraries -> React.xcodeproj -> Build Settings -> Add user defined setting

i added this and it solved my problem

Sebastien H.
  • 5,543
  • 2
  • 24
  • 32
0

In react-native: 0.60.5

Go to > [Your Project] > ios > [Your Project].xcodeproj > project.pbxproj. Search for 8081 and replace all the port 8081 to 8089(example)

        shellScript = "export RCT_METRO_PORT=\"${RCT_METRO_PORT:=8089}\"\necho \"export RCT_METRO_PORT=${RCT_METRO_PORT}\" > \"${SRCROOT}/../node_modules/react-native/scripts/.packager.env\"\nif [ -z \"${RCT_NO_LAUNCH_PACKAGER+xxx}\" ] ; then\n  if nc -w 5 -z localhost ${RCT_METRO_PORT} ; then\n    if ! curl -s \"http://localhost:${RCT_METRO_PORT}/status\" | grep -q \"packager-status:running\" ; then\n      echo \"Port ${RCT_METRO_PORT} already in use, packager is either not running or not running correctly\"\n      exit 2\n    fi\n  else\n    open \"$SRCROOT/../node_modules/react-native/scripts/launchPackager.command\" || echo \"Can't start packager automatically\"\n  fi\nfi\n";
Ashish Singh Rawat
  • 961
  • 11
  • 25