"npx react-native start" 挂起

"npx react-native start" hangs

简介

我正在尝试使用 official docs 使用 React Native 0.63.3 创建一个 Hello World 风格的程序。我正在使用 React Native CLI Quickstart 路径 link.

到目前为止我做了什么

我已经进入了部分:
运行 你的 React Native 应用程序
第 1 步:启动 Metro

我 运行:

npx react-native start --verbose

我得到以下输出:

               ######                ######               
             ###     ####        ####     ###             
            ##          ###    ###          ##            
            ##             ####             ##            
            ##             ####             ##            
            ##           ##    ##           ##            
            ##         ###      ###         ##            
             ##  ########################  ##             
          ######    ###            ###    ######          
      ###     ##    ##              ##    ##     ###      
   ###         ## ###      ####      ### ##         ###   
  ##           ####      ########      ####           ##  
 ##             ###     ##########     ###             ## 
  ##           ####      ########      ####           ##  
   ###         ## ###      ####      ### ##         ###   
      ###     ##    ##              ##    ##     ###      
          ######    ###            ###    ######          
             ##  ########################  ##             
            ##         ###      ###         ##            
            ##           ##    ##           ##            
            ##             ####             ##            
            ##             ####             ##            
            ##          ###    ###          ##            
             ###     ####        ####     ###             
               ######                ######               
                                                          
debug Checking for a newer version of React Native
debug Current version: 0.63.3
debug Cached release version: 0.60.0
debug Checking for newer releases on GitHub
                 Welcome to React Native!
                Learn once, write anywhere



To reload the app press "r"
To open developer menu press "d"

debug Latest release: 0.60.0

按“r”得到以下结果:

No apps connected. Sending "reload" to all React Native apps failed. Make sure your app is running in the simulator or on a phone connected via USB.
info Reloading app...

按“d”得到以下结果:

warn No apps connected. Sending "devMenu" to all React Native apps failed. Make sure your app is running in the simulator or on a phone connected via USB.
info Opening developer menu...

问题

如何成功 运行 并完成 Metro 进程,以便我可以 运行 我的 React Native 应用程序?

使用 npx react-native start,您正在 运行 构建,但是,对于 运行 应用程序,您需要选择 运行 androidiossource:

android:

npx react-native run-android

ios: (mac)

npx react-native run-ios

正在关注 Set Up React-Native on Your WSL2 for Android DevelopmentBuilding a react native app in WSL2

但是我也有同样的问题

要重新加载应用程序,请按“r” 要打开开发者菜单,请按“d” 警告 未连接任何应用程序。向所有 React Native 应用程序发送“重新加载”失败。确保您的应用在模拟器中 运行 或在通过 USB 连接的 phone 上。 正在重新加载应用程序...

解决方案 - Result

  1. 创建资产文件夹

遵循此 link:此错误通常发生是因为系统无法创建包含应用程序中所有 Javascript 代码的初始包。要解决这个问题,首先要在 react-native 项目文件夹中创建一个资产文件夹:

  • 转到 wsl 中的项目文件夹或 ubuntu

cd android/app/src/main/

  • 创建一个名为 assets 的文件夹

mkdir assets

  • 创建包含所有 Javascript 代码的初始包 按照这个 link 到

    [your project]/android/app/src/main$ npx react-native bundle --platform android --dev false --entry-file index.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/src/main/res

  1. 如果 adb 使用环境变量配置崩溃,请使用 socat
  • 运行 在 PowerShell 中:

adb kill-server ;adb -a -P 5037 nodaemon server

  • 运行 在 ubuntu 或 wsl:

socat -d -d TCP-LISTEN:5037,reuseaddr,fork TCP:$(cat /etc/resolv.conf | tail -n1 | cut -d " " -f 2):5037

  • 运行 在 NEW PowerShell 中:

$WSL_CLIENT = bash.exe -c "ip addr show eth0 | grep -oP '(?<=inet\s)\d+(.\d+){3}'"

然后

iex "netsh interface portproxy add v4tov4 listenport=8081 listenaddress=127.0.0.1 connectport=8081 connectaddress=$WSL_CLIENT"