'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
I have tried a few options mentioned at:
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
Solution 1:[1]
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
}
...
}
Solution 2:[2]
The simplest solution is:
The below command will build Android or iOS package which will listen to port 1234
For iOS:
react-native run-ios --port=1234
For Android
react-native run-android --port=1234
If you are using metro-server then you can add port under server object like :
server:{
port:1234
}
or
run
react-native start --port=1234
Find out more configuration for metro-server here: https://facebook.github.io/metro/docs/en/configuration
But requires 0.55 and above.
Solution 3:[3]
After spending a whole day and going through many solutions, a combination of the suggestions helped me resolve this. Follow the steps given below:
Create the project using the command: 'react-native init [PROJECT_NAME]'
Open the project in Xcode and replace all occurrences of "8081" with "8088" and save the changes
Open terminal and change the working directory to the above created project directory. Use the following command to change the port that react native uses: react-native start --port 8088
Once you run this command, you see the following output in the terminal:
As you can see, this starts the Metro instance. Do not kill the command or the terminal window. Let this process run.
- Open a new terminal window, change the working directory to the project directory and run the react-native project using the command:
react-native run-ios
Once the project builds successfully on the second terminal, you will see a progress bar indicating the loading of the app bundle in the first terminal window as shown below:
On completion of loading the bundle, the app succesfully launches on the simulator
Hope this helps. Happy coding
Solution 4:[4]
run metro-bundler server with specified port eg. "8089"
react-native start --port 8089
build iOS and Android package which listens to above port
iOS:. react-native run-ios --port 8089 --simulator \"iPhone 8\"
Android: react-native run-android --port 8089
Change the server and port number in Dev-settings after launching the app on simulator or device.
- iOS:
Command + D in Mac and Ctrl + D in windows- Click on Configure Bundler option
- provide host as
localhostand port as8089then apply changes
- Android:
Command + M in Mac and Ctrl + M in windows- Click on Change Bundle Location
- change it to
localhost:8089then press ok
Solution 5:[5]
You need to overwrite the RCT_METRO_PORT macro variable to ensure your app points to the correct port when running via xcode or react-native run-ios. This can be done by opening the Pods project within your workspace, navigating to Build Settings and adding a Preprocessor Macro. For example RCT_METRO_PORT=7777, if the port you are using is 7777.
Solution 6:[6]
Set RCT_METRO_PORT, example:
export RCT_METRO_PORT=8765
Solution 7:[7]
export RCT_METRO_PORT=8082
react-native start --port $RCT_METRO_PORT
react-native run-ios
https://facebook.github.io/react-native/docs/troubleshooting#using-a-port-other-than-8081
Solution 8:[8]
/**
* Metro configuration for React Native
* https://github.com/facebook/react-native
*
* @format
*/
module.exports = {
transformer: {
getTransformOptions: async () => ({
transform: {
experimentalImportSupport: false,
inlineRequires: false,
},
}),
},
server: {
port: 8088,
},
}
Solution 9:[9]
Actually, in the current version of React Native, configs of metro bundler are in the @react-native-community/cli and for changing the default PORT of metro bundler we should change the default PORT just by export an environment variable by the following command inside the project path:
export RCT_METRO_PORT=8590
Then in the ios folder of your project find the Pods folder and inside the Pods folder seek RCTDefines.h files, there are two of them, inside both of them change the 8081 to 8590.
Surely, for a test we can run the echo $RCT_METRO_PORT and if you see the new PORT 8590, it is changed now and we can run our project with default commands like yarn start and then yarn ios or yarn android.
Hint: for connecting to the React Native Debugger press ?+t and change the 8081 port to 8590.
Solution 10:[10]
You can use this react-native-port-patcher which replaces the default 8081 port with your desired port number.
Solution 11:[11]
Follow the steps :
Step 1:
vim node_modules/react-native/local-cli/server/server.js
Step 2 : change the default port - any other port //example -> 8089
Step 3 : go back to the project -> and do npm start
Solution 12:[12]
If using yarn- yarn start --port your port name worked for me. Eg. yarn start --port 8082
Solution 13:[13]
If you want to change the port other than 8081 and running the same in emulator, i think this link has better working solution : https://medium.com/@hsuastegui/use-react-native-in-a-different-port-1109db5674d8
Solution 14:[14]
When you excute command like
./node_modules/react-native/scripts/launchPackager.command
to start a debug server,append args like
---port 8082
it works for me in React native 0.53.3.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow



