【问题标题】:Can't run react native project for ios无法为 ios 运行 react 本机项目
【发布时间】:2020-01-27 07:30:47
【问题描述】:

我正在开发 react native 应用程序,我是 react-native 的新手,首先我为 android 平台工作并成功构建了 apk 文件。 现在我想使用我的 mackbook 中的命令 react-native run-ios 为 ios 版本运行它,但它会引发错误:

注意:从一开始我就在 macbook 上开发了这个应用程序

user-MacBook-Pro:myApp user$ react-native run-ios
error Cannot read property 'podfile' of null. Run CLI with --verbose flag for more details.
TypeError: Cannot read property 'podfile' of null
    at warnAboutPodInstall (/Users/user/project/myApp/node_modules/@react-native-community/cli-platform-ios/build/link/warnAboutPodInstall.js:43:90)
    at Object.runIOS [as func] (Users/user/project/myApp/node_modules/@react-native-community/cli-platform-ios/build/commands/runIOS/index.js:80:36)
    at Command.handleAction (Users/user/project/myApp/node_modules/react-native/node_modules/@react-native-community/cli/build/index.js:164:23)
    at Command.listener (Users/user/project/myApp/node_modules/commander/index.js:315:8)
    at Command.emit (events.js:198:13)
    at Command.parseArgs (Users/user/project/myApp/node_modules/commander/index.js:651:12)
    at Command.parse (Users/user/project/myApp/node_modules/commander/index.js:474:21)
    at setupAndRun (Users/user/project/myApp/node_modules/react-native/node_modules/@react-native-community/cli/build/index.js:237:24)
    at Object.run (Users/user/project/myApp/node_modules/react-native/node_modules/@react-native-community/cli/build/index.js:184:11)
    at Object.<anonymous> (/usr/local/lib/node_modules/react-native-cli/index.js:117:7)

播客文件

 platform :ios, '9.0'
require_relative '../node_modules/@react-native-community/cli-platform-ios/native_modules'

 target 'myApp' do
  # Pods for myApp
  pod 'FBLazyVector', :path => "../node_modules/react-native/Libraries/FBLazyVector"
  pod 'FBReactNativeSpec', :path => "../node_modules/react-native/Libraries/FBReactNativeSpec"
  pod 'RCTRequired', :path => "../node_modules/react-native/Libraries/RCTRequired"
  pod 'RCTTypeSafety', :path => "../node_modules/react-native/Libraries/TypeSafety"
  pod 'React', :path => '../node_modules/react-native/'
  pod 'React-Core', :path => '../node_modules/react-native/'
  pod 'React-CoreModules', :path => '../node_modules/react-native/React/CoreModules'
  pod 'React-Core/DevSupport', :path => '../node_modules/react-native/'
  pod 'React-RCTActionSheet', :path => '../node_modules/react-native/Libraries/ActionSheetIOS'
  pod 'React-RCTAnimation', :path => '../node_modules/react-native/Libraries/NativeAnimation'
  pod 'React-RCTBlob', :path => '../node_modules/react-native/Libraries/Blob'
  pod 'React-RCTImage', :path => '../node_modules/react-native/Libraries/Image'
  pod 'React-RCTLinking', :path => '../node_modules/react-native/Libraries/LinkingIOS'
  pod 'React-RCTNetwork', :path => '../node_modules/react-native/Libraries/Network'
  pod 'React-RCTSettings', :path => '../node_modules/react-native/Libraries/Settings'
  pod 'React-RCTText', :path => '../node_modules/react-native/Libraries/Text'
  pod 'React-RCTVibration', :path => '../node_modules/react-native/Libraries/Vibration'
  pod 'React-Core/RCTWebSocket', :path => '../node_modules/react-native/'

  pod 'React-cxxreact', :path => '../node_modules/react-native/ReactCommon/cxxreact'
  pod 'React-jsi', :path => '../node_modules/react-native/ReactCommon/jsi'
  pod 'React-jsiexecutor', :path => '../node_modules/react-native/ReactCommon/jsiexecutor'
  pod 'React-jsinspector', :path => '../node_modules/react-native/ReactCommon/jsinspector'
  pod 'ReactCommon/jscallinvoker', :path => "../node_modules/react-native/ReactCommon"
  pod 'ReactCommon/turbomodule/core', :path => "../node_modules/react-native/ReactCommon"
  pod 'Yoga', :path => '../node_modules/react-native/ReactCommon/yoga'

  pod 'DoubleConversion', :podspec => '../node_modules/react-native/third-party-podspecs/DoubleConversion.podspec'
  pod 'glog', :podspec => '../node_modules/react-native/third-party-podspecs/glog.podspec'
  pod 'Folly', :podspec => '../node_modules/react-native/third-party-podspecs/Folly.podspec'

  target 'myAppTests' do
    inherit! :search_paths
    # Pods for testing
  end

  use_native_modules!
end

target 'myApp-tvOS' do
  # Pods for myApp-tvOS

  target 'myApp-tvOSTests' do
    inherit! :search_paths
    # Pods for testing
  end

end

请帮助知道解决方案的我。 提前致谢。

【问题讨论】:

    标签: ios react-native cocoapods


    【解决方案1】:
    1. 打开终端
    2. 将 ios 文件夹移至 React-Native 项目下
    3. 运行 pod install 命令 - 这将为已安装的需要链接的 rpm 模块安装所有链接库。
    4. 然后运行 ​​react-native run-ios 或双击projectName.xcworkspace,这将打开Xcode。
    5. 选择设备来运行和播放项目。

    【讨论】:

    • 感谢您的回答:从 ios 目录运行命令 pod install 时会发生这种情况.. user-MacBook-Pro:ios user$ pod install [!] Invalid Podfile file: undefined method `[ ]' 为 nil:NilClass。 # 来自 /Users/user/project/myApp/ios/Podfile:42 # --------------------------------- ---------- # > use_native_modules! # end # -------------------------------------------
    【解决方案2】:
    • 首先您需要了解 React Native 的基础知识。您可以从here查看官方说明如何在设备上运行 React Native 项目

    • 但在 iOS 上运行应用程序之前,您需要做一些额外的事情


    • 导航到项目根文件夹中的 ios 文件夹
    • 从活动位置打开终端并运行此命令

    吊舱安装

    • 如果您需要更多关于 cocoapods 的信息,请访问this

    • 然后打开 XCODE 并导航到 ios 文件夹并打开您的项目文件,其扩展名为 .xcworkspace

    MyProject.xcworkspace

    • 现在您可以通过 XCODE 运行您的 iOS 应用程序,但在运行应用程序之前,请确保您已清理构建文件夹 (Command+Shift+K) 并重新构建 (Command+B) strong> 使用 XCODE 的项目。
    • 最后选择设备并从 XCODE 运行您的应用程序。 (Command+R)

    【讨论】:

    • 感谢您的回答:从 ios 目录运行命令 pod install 时会出现此错误-> user-MacBook-Pro:ios user$ pod install [!] Invalid Podfile file: undefined method ` []' 为 nil:NilClass。 # 来自 /Users/user/project/myApp/ios/Podfile:42 # --------------------------------- ---------- # > use_native_modules! # end # -------------------------------------------
    • @BellalHossain 看到这个问题 -> github.com/react-native-community/cli/issues/860
    【解决方案3】:

    在终端中,从您的顶级项目目录运行以下命令:

    cd ios # <-- To get into your ios subdirectory
    ls
    

    您应该会看到一个名为“Podfile”的文件,如下所示:

    drwxr-xr-x   8 r  staff    256 Sep 21 14:04 AwesomeTSProject
    -rw-r--r--   1 r  staff    749 Sep 21 14:04 Podfile
    

    接下来运行以下命令:

    pod install
    

    您应该会看到创建的文件如下:

    drwxr-xr-x   4 r  staff    128 Sep 21 14:04 AwesomeTSProjectTests
    drwxr-xr-x@  5 r  staff    160 Sep 21 14:22 AwesomeTSProject.xcworkspace
    drwxr-xr-x  10 r  staff    320 Sep 29 10:00 .
    drwxr-xr-x@  4 r  staff    128 Sep 29 10:19 AwesomeTSProject.xcodeproj
    -rw-r--r--   1 r  staff  19077 Sep 29 10:19 Podfile.lock
    -rw-r--r--@  1 r  staff   6148 Sep 30 04:00 .DS_Store
    drwxr-xr-x  26 r  staff    832 Jan  9 09:01 Pods
    drwxr-xr-x  25 r  staff    800 Jan  9 09:42 ..
    

    只要您没有看到任何错误,您现在就可以运行以下命令:

    cd ..  
    

    您应该会从 ios 文件夹中弹出并向上移动到您的父项目目录。

    npx react-native run-ios
    

    如果一切顺利,您应该会在您的应用中看到一个 iOS 模拟器。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-05
      • 1970-01-01
      • 2021-12-20
      • 1970-01-01
      • 2017-06-02
      • 1970-01-01
      • 2021-10-01
      • 2016-05-02
      相关资源
      最近更新 更多