Is there an easy way to change the default port when i type in react-native run-android?

巧了我就是萌 提交于 2019-12-13 07:45:29

问题


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


来源:https://stackoverflow.com/questions/45582514/is-there-an-easy-way-to-change-the-default-port-when-i-type-in-react-native-run

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!