React Native从0.66.3升级至0.68.2时执行pod install触发use_native_modules!相关错误求助
The error you're encountering is almost certainly tied to a version mismatch between the React Native CLI and your project's target React Native version. Here's how to resolve it step by step:
1. Downgrade the React Native CLI to a compatible version
Your project currently uses @react-native-community/cli@8.0.2, which is built for React Native 0.70+. React Native 0.68.2 requires CLI version 7.0.3 to function correctly.
Update your package.json devDependencies section:
"@react-native-community/cli": "7.0.3"
Then reinstall your dependencies to apply the change:
rm -rf node_modules yarn.lock yarn install
2. Verify your Podfile setup
Ensure your ios/Podfile matches the standard structure for React Native 0.68.2:
- At the top, include these required imports:
require_relative '../node_modules/react-native/scripts/react_native_pods' require_relative '../node_modules/@react-native-community/cli-platform-ios/native_modules' - Confirm the minimum iOS platform version is set correctly (RN 0.68 requires iOS 12.4+):
platform :ios, '12.4' - Make sure the
use_native_modules!call is placed inside your target block:target 'app' do config = use_native_modules! # Rest of your target configuration goes here end
3. Clear caches and perform a fresh pod install
After updating dependencies and fixing the Podfile, clear all cached data to eliminate any leftover conflicts:
# Clean node modules and yarn cache rm -rf node_modules yarn cache clean # Clean iOS pod artifacts cd ios pod deintegrate pod cache clean --all rm -rf Pods Podfile.lock # Reinstall pods (use arch -x86_64 for M1/M2 chips) arch -x86_64 pod install
4. Double-check native module compatibility
While the CLI mismatch is the primary cause, confirm your native dependencies support React Native 0.68.2:
- Most of your listed packages (like
react-native-svg,react-native-vector-icons) have versions compatible with RN 0.68, but if you hit issues later, consider updating to their latest compatible releases. - Avoid deprecated tools like
react-native-ejectwhich can introduce unexpected conflicts.
Why this happens
The use_native_modules! helper in CLI v8 includes breaking changes that don't align with React Native 0.68's internal setup. When the CLI tries to access configurations from an undefined object (caused by the version mismatch), it throws the error you're seeing. Downgrading the CLI to the correct version restores the expected behavior of this helper function.
内容的提问来源于stack exchange,提问作者Gergő Horváth

