Is there an easy way to change the default port wh

2019-09-16 19:31发布

问题:

My steps to change the default port:

  1. react-native start --port 9999.
  2. react-native run-android.
  3. 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-portat 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:-

  1. You can directly run below command at project level.
  2. you can also update port number replace 8082 with your port number.
  3. For Android you need to set port in dav setting/Debug Server host & port for device then set port like 127.1.0.0:8082 (first your ip and then use your port).
  4. 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