Reputation: 271584
I want to run my react native app twice: my device + simulator.
I don't mind using two metro bundler instances. How can I do that?
Right now, both my devices connect to 8081 - and whenever one connects, it kicks the other one off.
(I first built from XCode to my device/simulator, and then I run react-native start
)
Upvotes: 9
Views: 13702
Reputation: 1253
Everything is much eseaier,
adb devices
in terminal. It will show your device idsList of devices attached
RKCB91214AA device
emulator-1258 device
npx react-native run-android --deviceId="RKCB91214AA" --port=8082
and npx react-native run-android --deviceId="emulator-1258" --port=8081
for each device on different terminals. Just make sure that commands have different ports.React Native DevMenu
(by shaking phone) on RKCB91214AA
device and change Bundle Location
to localhost:8082
Upvotes: 0
Reputation: 21
tldr; You need to run metro on a different server and then open the simulator device on that server, then from dev tools and change the port&host
For this answer I am assuming one of your React Native Application is running fine on default server. Steps to run another application -
Step 1: run metro-bundler server with specified port
npx react-native start --port=8088
Step 2: run the device on same server
iOS: npx react-native run-ios --port=8088 --simulator="iPhone 13"
Android: npx react-native run-android --port=8088
(writing the same without equal sign also works)
npx react-native run-ios --port 8088 --simulator "iPhone 13"
Step 3: Change the host server and port number in dev tools after launching the app
iOS: Command + D (or ctrl+cmd+Z) in Mac and Ctrl + D in windows Click on Configure Bundler option provide host as localhost (or 127.0.0.1) and port as 8088 then click apply changes
Android: Command + M in Mac and Ctrl + M in windows Click on Change Bundle Location change it to localhost:8088 then press ok
Note1-if you are running your app on your phone then provide its address as mentioned in Kasra's answer above
NOTE2- Sometimes what I experienced was that shake keypress(cmd+d or ctrl+cmd+z) which you need to access the configure bundler option on iOS was stuck, it simply didn't respond whatever shortcut I tried, even opening it from menubar didn't work, in that case, close the app and open again using command from step2, and now open the terminal window in which metro is running, there will be two options press d and press r, if the active window is terminal window then shake option will work after pressing d , attaching my VScode terminal screenshot for reference, you may have metro running in any terminal this thing took a 2 hours for me, hence I thought it's worth mentioning
Upvotes: 2
Reputation: 2189
npx react-native run-ios --device "Kasra’s iPhone"
, of course replace Kasra’s iPhone
with the name of your real ios device.npx react-native start --port 8082
Ctrl + D
to open the dev menu, or shake the device to open it.Configure Bundler
optionifconfig
or ipconfig
in terminal to find your IP based on your OS.8082
Apply Changes
, it should automatically start the app on your ios real devicenpx react-native run-ios
to run it on your simulator.Now you have two bundlers running side by side on ports 8081 and 8082 simultaneously.
Upvotes: 16
Reputation: 24660
react-native run-ios
command supports a port
parameter after this commit. You can try to use this parameter to run your app in two different ports with two different builds.
From commit notes:
adds
--port
option toreact-native run-ios
as well as patches port …Summary: The pull request adds the
--port
option torun-ios
allowing a developer to build and launch a react-native app using a single command line like this:react-native run-ios --port 8088
It defaults to the current port 8081.
Upvotes: 6