You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Expo EAS构建中强制Yarn 4+使用node-modules而非PnP?

解决EAS构建强制使用node-modules而非PnP的问题

问题场景

基于React Native开发、运行在Expo SDK 52的应用,使用Yarn 4.7版本,已做如下配置:

  • package.json中设置"packageManager": "yarn@4.7.0"
  • 配置Expo钩子"eas-build-pre-install": "corepack enable"以在EAS构建中启用corepack
  • yarnrc.yml中设置nodeLinker: node-modules

但执行构建命令NODE_ENV=production eas build --local --platform android时,出现两个错误:

Error: @expo/cli tried to access metro, but it isn't declared in its dependencies; this makes the require call ambiguous and unsound.
ConfigError: Cannot resolve entry file: The `main` field defined in your `package.json` points to an unresolvable or non-existent path.

经排查,Expo构建仍在使用PnP而非node-modules作为nodeLinker,本地构建和Expo.io远程构建均存在该问题。


解决步骤

1. 强制在EAS预安装阶段设置nodeLinker

修改package.json中的eas-build-pre-install钩子,在启用corepack后直接指定nodeLinker,避免配置文件读取异常:

"eas-build-pre-install": "corepack enable && yarn config set nodeLinker node-modules"

2. 通过环境变量覆盖EAS构建配置

在项目根目录的eas.json中,针对目标构建profile(比如production)添加环境变量,强制yarn使用node-modules:

"production": {
  "env": {
    "YARN_NODE_LINKER": "node-modules"
  },
  // 保留原有其他配置项
}

3. 验证本地配置并清理缓存

本地执行以下命令,确保依赖以node-modules模式安装:

# 验证当前生效的nodeLinker配置
yarn config get nodeLinker
# 清理缓存并重新安装
yarn cache clean
rm -rf node_modules
yarn install

4. 补充依赖解决@expo/cli的metro访问问题

作为辅助方案,手动将metro添加为项目开发依赖,避免PnP模式下的依赖访问歧义:

yarn add --dev metro

内容的提问来源于stack exchange,提问作者Pajala

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 12:02:35