使用Yarn 2创建Expo React Native项目启动时依赖报错
解决Yarn 2环境下Expo项目
yarn start时的glob依赖缺失错误 运行yarn start时报错如下:
PS D:\Files\Projects\my-rn-journey\other-navigators> yarn start D:\Files\Projects\my-rn-journey\other-navigators\.pnp.cjs:21723 Error.captureStackTrace(firstError); ^ Error: @expo/cli tried to access glob, but it isn't declared in its dependencies; this makes the require call ambiguous and unsound. Required package: glob Required by: @expo/cli@npm:0.7.3 (via D:\Files\Projects\my-rn-journey\other-navigators\.yarn\cache\@expo-cli-npm-0.7.3-b8fe3aaa8f-f1b03efbec.zip\node_modules\@expo\cli\build\src\utils\) Require stack: - D:\Files\Projects\my-rn-journey\other-navigators\.yarn\cache\@expo-cli-npm-0.7.3-b8fe3aaa8f-f1b03efbec.zip\node_modules\@expo\cli\build\src\utils\glob.js - D:\Files\Projects\my-rn-journey\other-navigators\.yarn\cache\@expo-cli-npm-0.7.3-b8fe3aaa8f-f1b03efbec.zip\node_modules\@expo\cli\build\src\start\doctor\typescript\TypeScriptProjectPrerequisite.js - D:\Files\Projects\my-rn-journey\other-navigators\.yarn\cache\@expo-cli-npm-0.7.3-b8fe3aaa8f-f1b03efbec.zip\node_modules\@expo\cli\build\src\start\startAsync.js - D:\Files\Projects\my-rn-journey\other-navigators\.yarn\cache\@expo-cli-npm-0.7.3-b8fe3aaa8f-f1b03efbec.zip\node_modules\@expo\cli\build\src\start\index.js - D:\Files\Projects\my-rn-journey\other-navigators\.yarn\cache\@expo-cli-npm-0.7.3-b8fe3aaa8f-f1b03efbec.zip\node_modules\@expo\cli\build\bin\cli at require$$0.Module._resolveFilename (D:\Files\Projects\my-rn-journey\other-navigators\.pnp.cjs:21723:13) at Module._load (node:internal/modules/cjs/loader:922:27) at require$$0.Module._load (D:\Files\Projects\my-rn-journey\other-navigators\.pnp.cjs:21612:31) at Module.require (node:internal/modules/cjs/loader:1143:19) at require (node:internal/modules/cjs/helpers:110:18) at Object.<anonymous> (D:\Files\Projects\my-rn-journey\other-navigators\.yarn\cache\@expo-cli-npm-0.7.3-b8fe3aaa8f-f1b03efbec.zip\node_modules\@expo\cli\build\src\utils\glob.js:8:13) at Module._compile (node:internal/modules/cjs/loader:1256:14) at Module._extensions..js (node:internal/modules/cjs/loader:1310:10) at require$$0.Module._extensions..js (D:\Files\Projects\my-rn-journey\other-navigators\.pnp.cjs:21767:33) at Module.load (node:internal/modules/cjs/loader:1119:32) Node.js v18.16.1
解决方案
方法一:直接安装glob依赖
在项目根目录执行命令:
yarn add glob
安装完成后重新运行yarn start即可。
方法二:通过Yarn配置补充依赖声明
如果不想将glob添加到项目直接依赖,可以修改项目根目录的.yarnrc.yml文件,添加以下配置:
packageExtensions: "@expo/cli@*": dependencies: glob: "*"
保存后执行yarn install,再启动项目。
原因说明
Yarn 2的Plug'n'Play(PnP)模式会严格校验包的依赖声明,@expo/cli@0.7.3内部使用了glob库,但未在自身的dependencies中声明该依赖,导致PnP无法解析这个隐式依赖,从而抛出错误。上述两种方法都能让Yarn正确识别并加载glob库,解决启动报错问题。
内容的提问来源于stack exchange,提问作者Aşkın Kadir Çekim
相关产品推荐
相关产品推荐

