React Native - iOS - 无效的"Podfile"文件:nil:NilClass 的未定义方法 '[]'



我刚刚开始使用 React Native。我遵循了官方文档中的 React Native CLI 快速入门。这也意味着我使用的是 React Native 版本0.61.2

从那以后,我做了一些基本的UI,在iPhone模拟器中检查结果。它在运行react-native run-ios时工作,并没有真正接触ios/Pod文件太多。

我的问题是在我的ios/文件夹中运行pod安装总是导致错误!现在这成为一个问题,我需要安装其他软件包。但是它已经失败了默认的 Podfile!

错误信息:

[!] Invalid `Podfile` file: undefined method `[]' for nil:NilClass.
#  from /Users/marcel/Projekte/WerBinIch/ios/Podfile:43
#  -------------------------------------------
#  
>    use_native_modules!
#  end
#  -------------------------------------------

ios/podfile

platform :ios, '9.0'
require_relative '../node_modules/@react-native-community/cli-platform-ios/native_modules'
target 'WerBinIch' do
# Pods for WerBinIch
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 'WerBinIchTests' do
inherit! :search_paths
# Pods for testing
end
use_native_modules!
end

我不知道如何处理这个问题!我不知道 Podfile 和 xcode 文件的东西是如何工作的!我正在尝试删除文件夹以完成重置。更重要的是,我不知道错误消息可能意味着什么。

当您 cd 到 ios 文件夹并运行"pod install"时会发生此错误 这是因为您通过以下方式添加了一个新的 React 本机模块

npm -i

而不是使用纱线

加纱

要解决此问题,只需转到 package.json 所在的主 react 本机项目文件夹并再次运行以下命令

纱线缓存清洁

加纱

然后尝试将CD连接到ios文件夹并运行"pod安装",就不会再有错误了。希望这可以帮助其他同样面临同样问题的人。

这个答案很好地解决了我的问题:

反应原生 iOS Podfile 问题与"use_native_modules!

这是正确答案:

1 - 您的Podfile顶部应包含此行

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

2 - 确保您的 package.json 和node_module文件夹安装了此模块

cli-platform-ios/native_modules

3 - 如果在运行yarn install后没有找到 - 意味着您的计算机中有旧的缓存node_modules,您需要在重新安装软件包之前清理它。

4 - 清理缓存

$ yarn cache clean

5 - 确保您react-native.config.js此文件并且其配置有效 - 并且它没有不存在的 NPM 包 - 此步骤是最后也是主要导致错误的原因

我的react-native.config.js示例

module.exports = {
project: {
ios: {},
android: {},
},
assets: ['./assets/fonts/'],
dependencies: {}, // make sure this deps are all valid installed packages or empty if you don't need it
};

6 -yarn install安装节点包,您的 Pod 现在应该可以工作了!$ pod install --repo-update

祝您编码愉快!

半天后尝试所有解决方案,最后,我替换了两者

  • react-native
  • @react-native-community

文件夹从另一个 RN 0.63 项目到 node_modules 并已解决。 也许我的缓存未正确删除或替换。

就我而言,我不小心在我的iOS文件夹中创建了一个package.json。这似乎混淆了反应本机构建。我删除了ios文件夹中的package.json,将package.json留在根项目文件夹中,一切正常。

  • 添加"@react-native-community/cli-platform-ios": "3.0.0"到您的 package.json 文件中并在主目录中运行npm install
  • 然后cd ios并运行pod install

最新更新