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

使用Yarn PNP启动React项目时出现@babel/preset-env依赖缺失报错

Yarn PNP启动React项目时出现@babel/preset-env依赖缺失报错

最近我开始更新一个多年未维护的React应用的依赖包,同时把Yarn从4.1.0升级到了4.6.0,还在.yarnrc.yml文件中添加了nodeLinker: pnp配置。现在执行yarn start启动应用时,出现了以下错误:

src\useMyHook.ts
  Line 0:  Parsing error: Your application tried to access @babel/preset-env, but it isn't declared in your dependencies; this makes the require call ambiguous and unsound.        

Required package: @babel/preset-env (via "@babel\preset-env")
Required by: app\ui\

Require stack:
- app\ui\.yarn\cache\@babel-core-npm-7.19.0-50eaedd5da-60a01c89e8.zip\node_modules\@babel\core\lib\config\files\index.js
- app\ui\.yarn\cache\@babel-core-npm-7.19.0-50eaedd5da-60a01c89e8.zip\node_modules\@babel\core\lib\config\files\plugins.js
- app\ui\.yarn\cache\@babel-core-npm-7.19.0-50eaedd5da-60a01c89e8.zip\node_modules\@babel\core\lib\index.js
- app\ui\.yarn\__virtual__\@rollup-plugin-babel-virtual-48b162daea\0\cache\@rollup-plugin-babel-npm-5.3.1-6039a4d033-eb3ee5fedd.zip\node_modules\@rollup\plugin-babel\dist\index.js
- app\ui\.yarn\cache\workbox-build-npm-6.5.4-71d02fae43-147373efa3.zip\node_modules\workbox-build\build\lib\bundle.js
- app\ui\.yarn\__virtual__\workbox-webpack-plugin-virtual-83d0dca87d\0\cache\workbox-webpack-plugin-npm-6.5.4-3842d0960f-12e2990df0.zip\node_modules\workbox-webpack-plugin\build\generate-sw.js
- app\ui\.yarn\__virtual__\workbox-webpack-plugin-virtual-83d0dca87d\0\cache\workbox-webpack-plugin-npm-6.5.4-3842d0960f-12e2990df0.zip\node_modules\workbox-webpack-plugin\build\index.js
- app\ui\.yarn\__virtual__\react-scripts-virtual-2cc3c53625\0\cache\react-scripts-npm-5.0.1-d06bd2d5ad-1727f9b460.zip\node_modules\react-scripts\config\webpack.config.js
- app\ui\.yarn\__virtual__\react-scripts-virtual-2cc3c53625\0\cache\react-scripts-npm-5.0.1-d06bd2d5ad-1727f9b460.zip\node_modules\react-scripts\scripts\start.js

奇怪的是,如果我把配置改成nodeLinker: node-modules,就不会出现这个错误。这肯定是Yarn PNP存在兼容性问题(我从相关技术讨论中得到切换的思路)。

我想知道为什么会出现这个错误,因为我更倾向于使用nodeLinker: pnp配置,这个问题似乎和react-scripts有关?

补充说明:这个问题是在我把ESLint从7版本迁移到8版本后开始出现的,同时我还把配置从JSON格式改成了使用eslint.config.js的扁平配置,并移除了package.json中的eslintConfig字段。

备注:内容来源于stack exchange,提问作者Tirca Mihnea

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.14 15:24:29