问题
My steps to change the default port:
- react-native start --port 9999.
- react-native run-android.
- change the port in the app.
Is there an easy way?
回答1:
You can also change permanent the port with change default server in react native node modules your_app\node_modules\react-native\local-cli\server\server.js
and change the port 8081 to 9999 like this :
....
module.exports = {
name: 'start',
func: server,
description: 'starts the webserver',
options: [{
command: '--port [number]',
default: 8081, //change to 9999
parse: (val: string) => Number(val),
},
....
I hope this answer can help you :)
回答2:
Update your port of your react-native project by below commands. just save below code in file can be named as update-port
and run this file by /file/path/update-port
at your react-native project level (Where you run npm start
or yarn start
then no need to change anything just run yarn start
and your code automatically run on 8082
.
Note:-
- You can directly run below command at project level.
- you can also update port number replace
8082
with your port number. - For Android you need to set port in
dav setting/Debug Server host & port for device
then set port like127.1.0.0:8082
(first your ip and then use your port). - When ever your update your node-module the default port will automatically change then please re-run
update-port
file.
#!/bin/bash
sed -i '' 's/8081/8082/g' ./node_modules/react-native/local-cli/server/server.js
sed -i '' 's/8081/8082/g' ./node_modules/react-native/React/Base/RCTBridgeDelegate.h
sed -i '' 's/8081/8082/g' ./node_modules/react-native/React/Base/RCTDefines.h
sed -i '' 's/8081/8082/g' ./node_modules/react-native/React/DevSupport/RCTInspectorDevServerHelper.mm
sed -i '' 's/8081/8082/g' ./node_modules/react-native/React/React.xcodeproj/project.pbxproj
sed -i '' 's/8081/8082/g' ./node_modules/react-native/local-cli/runIOS/runIOS.js
sed -i '' 's/8081/8082/g' ./node_modules/react-native/local-cli/util/isPackagerRunning.js
sed -i '' 's/8081/8082/g' ./node_modules/react-native/local-cli/runAndroid/runAndroid.js
sed -i '' 's/8081/8082/g' ./node_modules/react-native/Libraries/RCTTest/RCTTestRunner.m
来源:https://stackoverflow.com/questions/45582514/is-there-an-easy-way-to-change-the-default-port-when-i-type-in-react-native-run