第一个react native的运行

以下用的是react native cli的方式。如果使用Expo Go,请参考:https://reactnative.dev/docs/environment-setup?guide=quickstart

1. 安装cli命令:

   npm uninstall -g react-native-cli
   npm uninstall -g create-react-app
   npm install -g react-native

2. 尝试运行:

   react-native init myFirstProject

   结果报错误1 , 就升级ruby to 3(通过安装rvm),安装cocoapods

3. 再次运行 react-native init myFirstProject ,依然报错,见错误2 , 安装watchman

4. 卸载了global安装的react-native-cli ,使用npx react-native@latest init AwesomeProject来新建项目。

   If you previously installed a global react-native-cli package, please remove it as it may cause unexpected issues:

npm uninstall -g react-native-cli @react-native-community/cli

5. 这时还有错误2, 于是我又在xcode中设置了xcode command tools,和 IOS Simulator,又确保xcode command tools设置正确了。再次运行:

    npx react-native@latest init AwesomeProject

   这时才成功了。

    可以用 npx react-native info 来看看当前最新的react native的版本信息

6. 创建项目并build成功后。
    npx react-native start     // starts Metro Bundler (也可以不执行, 但执行了后启动Metro)
    npx react-native run-ios     // go to your project folder

 

运行的时候也可以用XCode打开项目下的ios文件夹,打开 app..xcodeproj 或 app..xcworkspace , 然后运行模拟器

----------------------------------------------- 遇到的错误列表 --------------------------------------

错误1:

mac上运行命令:react-native init myFirstProject --verbose   。 提示需要安装CocoaPods,

运行:sudo gem install cocoapods        报错如下:

ERROR: Error installing cocoapods:
The last version of activesupport (>= 5.0, < 8) to support your Ruby & RubyGems was 6.1.7.3. Try installing it with `gem install activesupport -v 6.1.7.3` and then running the current command again
activesupport requires Ruby version >= 2.7.0. The current ruby version is 2.6.10.210.

我选择了升级ruby到2.7,于是根据chatgpt的提示,我又参考了:https://stackoverflow.com/questions/60182199/upgrading-global-ruby-version-on-macos 和 https://rvm.io/rvm/install , 安装了rvm,然后安装2.7 ruby

gpg --keyserver keyserver.ubuntu.com --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
\curl -sSL https://get.rvm.io | bash -s stable --ruby
---- 安装完rvm后本来还要安装ruby 3,需要执行以下命令的:,结果发现ruby 3自动被安装了,可以通过rvm list查看,所以不用执行rvm install 3.2了。

rvm install (ruby version)

rvm --default use (ruby version)

can use rvm list to see available ruby versions.

# 安装 cocoapods

sudo gem install cocoapods

最后执行:react-native init myFirstProject --verbose ,还是错误,让我去到这里配置ios环境:https://reactnative.dev/docs/next/environment-setup

 ---- 错误2:

✖ Installing CocoaPods dependencies (this may take a few minutes)
error Error: Looks like your iOS environment is not properly set. Please go to https://reactnative.dev/docs/next/environment-setup and follow the React Native CLI QuickStart guide for macOS and iOS.
Error: Error: Looks like your iOS environment is not properly set. Please go to https://reactnative.dev/docs/next/environment-setup and follow the React Native CLI QuickStart guide for macOS and iOS.

于是参考 https://reactnative.dev/docs/next/environment-setup 中的“React Native CLI Quickstart”部分,执行:brew install watchman ,然后在XCode上执行一些操作:
1)安装Command Line Tools : xcode.setting -> Locations , 找到 “Command Line Tools”,选择版本,点下上下箭头确保设置好了。

2)Installing an iOS Simulator in Xcode:To install a simulator, open Xcode > Settings... (or Preferences...) and select the Platforms (or Components) tab. Select a simulator with the corresponding version of iOS you wish to use.

参考:
https://hackmd.io/@sandyuu/B1FP7VL1F

---- 错误3: 

有时候有这个错误:React Native Error: No bundle URL present , 和这个错误:cannot read property handleSetJSResponder of null 

参考:https://www.kindacode.com/article/react-native-no-bundle-url-present/#:~:text=When%20developing%20an%20iOS%20app%20with%20React%20Native%2C,have%20included%20a.jsbundle%20file%20in%20your%20application%20bundle.

我的解决方式是:a) cd ios  b) pod install  c) npm start  d) 在app上点击R,重新load app后结局。

---- 错误4: 

当运行expo start时,有时报这个错误:

opensslErrorStack: [ 'error:03000086:digital envelope routines::initialization error' ],
  library: 'digital envelope routines',
  reason: 'unsupported',
  code: 'ERR_OSSL_EVP_UNSUPPORTED'

临时解决办法:export NODE_OPTIONS=--openssl-legacy-provider

---- 错误5: 

执行expo start的时候,报这个错误:

Error running `xcrun simctl install 8CEB33A4-0A3D-444F-9C91-E4B428C2C529 /Users/peterfan/.expo/ios-simulator-app-cache/Exponent-2.23.2.tar.app`: An error was encountered processing the command (domain=NSPOSIXErrorDomain, code=22):
Simulator device returned an error for the requested operation.
The application's Info.plist does not contain a valid CFBundleVersion.
Ensure your bundle contains a valid CFBundleVersion.

参考: https://github.com/expo/expo-cli/issues/2413    and    https://stackoverflow.com/questions/51147704/expo-error-installing-or-running-app

但是对于我的测试来说,是因为:我terminal开启了代理proxy,导致下载expo tar包时有问题,没法部署到iphone simulator上的错误。

 

posted @ 2023-06-03 00:36  saaspeter  阅读(665)  评论(0编辑  收藏  举报