React native change listening port

前端 未结 13 1241
既然无缘
既然无缘 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:42

    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
       }
       ...
    }
    
    0 讨论(0)
  • 2020-12-05 17:47

    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

    0 讨论(0)
  • 2020-12-05 17:50
    /**
     * 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,
      },
    }
    
    0 讨论(0)
  • 2020-12-05 17:50

    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

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

    After spending a whole day and going through many solutions, a combination of the suggestions helped me resolve this. Follow the steps given below:

    1. Create the project using the command: 'react-native init [PROJECT_NAME]'

    2. Open the project in Xcode and replace all occurrences of "8081" with "8088" and save the changes

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

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

    0 讨论(0)
  • 2020-12-05 17:51
    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

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