使用Yarn Berry的React Native项目Pod安装失败求助
解决Yarn Berry 3.x/4.x + React Native 0.74.x Pod安装失败问题
问题核心
Yarn Berry默认采用PnP(Plug'n'Play)依赖管理模式,不会生成传统的node_modules目录,而是通过虚拟文件系统管理依赖。React Native默认Podfile中使用node require.resolve查找react-native/scripts/react_native_pods.rb的逻辑,在PnP模式下无法解析到包的物理路径,因此触发MODULE_NOT_FOUND错误。
解决方案
方案一:切换到Node Modules模式(简单直接)
- 在项目根目录创建或编辑
.yarnrc.yml文件,添加配置:nodeLinker: node-modules - 重新安装依赖:
yarn install - 进入iOS目录执行Pod安装:
cd ios && pod install
方案二:修改Podfile适配PnP模式(保留Yarn Berry特性)
替换Podfile开头的依赖解析代码:
原代码:
# Resolve react_native_pods.rb with node to allow for hoisting require Pod::Executable.execute_command('node', ['-p', 'require.resolve( "react-native/scripts/react_native_pods.rb", {paths: [process.argv[1]]}, )', __dir__]).strip
替换为:
# Resolve react-native path via Yarn to support PnP react_native_path = Pod::Executable.execute_command('yarn', ['dlx', 'yarn', 'pkg', 'resolve', 'react-native']).strip require File.join(react_native_path, 'scripts', 'react_native_pods.rb')
之后执行:
cd ios && pod install
验证
执行完上述任一方案后,若Pod安装过程无报错,且iOS项目能正常编译运行,则问题解决。
内容的提问来源于stack exchange,提问作者Hans C.
相关产品推荐
相关产品推荐

