React native change listening port

前端 未结 13 1243
既然无缘
既然无缘 2020-12-05 17:36

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

相关标签:
13条回答
  • 2020-12-05 17:54

    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 9095.

    0 讨论(0)
  • 2020-12-05 17:54

    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.

    0 讨论(0)
  • 2020-12-05 17:59

    Set RCT_METRO_PORT, example:

    export RCT_METRO_PORT=8765
    
    0 讨论(0)
  • 2020-12-05 18:03

    You can use this react-native-port-patcher which replaces the default 8081 port with your desired port number.

    0 讨论(0)
  • 2020-12-05 18:07

    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

    0 讨论(0)
  • 2020-12-05 18:07

    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.

    0 讨论(0)
提交回复
热议问题