Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React native change listening port

I am using react native android and having face issues to deploy the app on an android device. When I run

react-native start, it won't start dev server on port 8081

enter image description here

I have tried a few options mentioned at:

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

  2. Tried to stop the process running at port number 8081, but no success

My question is that can we change the React Native dev server port from 8081 (which is a default in android however the same we can change in ios from AppDelegate.m file) to something else or any other approach

Your responses will be highly appreciated. Thanks

like image 754
vikram jeet singh Avatar asked Dec 23 '15 07:12

vikram jeet singh


People also ask

How do I run react native in a specific port?

For AndroidOnce the emulator is running press CMD + m (Mac) or CTRL + m (Windows). Select Dev Settings and then Debug server host & port for device. Enter the url with the new port. Finally CMD + R or CTRL + R and the application should reload with the new bundle.

How do you change IP address in react native?

Go to application on devices. go to Dev Setting. Tap to Debug server host & port for device. Fill your IP address and port is 8081 (example ipaddress:8081) .


2 Answers

Not sure if this is documented or not[1], you can specify the port via a CLI argument, like this:

react-native start --port 9988 

I found it in the source code, and it worked on my local machine :)

https://github.com/facebook/react-native/blob/master/local-cli/server/server.js#L30


[1] This is now documented here: https://facebook.github.io/react-native/docs/troubleshooting#using-a-port-other-than-8081

like image 162
patrick Avatar answered Sep 21 '22 11:09

patrick


I know it is late but FYI, there is also another way where you can change your port permanently.

Go to your_app\node_modules\react-native\local-cli\server\server.js and change the port 8081 to 8088

which will be something like this

    ...       module.exports = {       name: 'start',       func: server,       description: 'starts the webserver',       options: [{         command: '--port [number]',         default: 8088,         parse: (val) => Number(val),       }      ... 

UPDATE TESTED ON RN 0.57:
1. If you are using custom metro config

const config = {   ...   server: {     port: 8088,   }   ... }; 

2. And if you are not then,
Go to your_app\node_modules\react-native\local-cli\util\Config.js

const Config = {    ...    server: {       port: process.env.RCT_METRO_PORT || 8088 //changed from 8081    }    ... } 
like image 34
Neel Gala Avatar answered Sep 20 '22 11:09

Neel Gala