create-react-app: 4.0.0-next.77 sample app yields "Module not found: Your application tried to access react-refresh" when started with Yarn 2
Describe the bug
create-react-app@4.0.0-next.77
yields “Module not found: Your application tried to access react-refresh, but it isn’t declared in your dependencies; this makes the require call ambiguous and unsound.” after running yarn start
with Yarn 2. Installing react-refresh
as dependency solves the problem, tho.
Environment
Environment Info:
current version of create-react-app: 4.0.0-next.77
running from /Users/v.kushnir/.npm/_npx/57508/lib/node_modules/create-react-app
System:
OS: macOS 10.15.6
CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
Binaries:
Node: 12.16.3 - /usr/local/bin/node
Yarn: 2.1.1 - /usr/local/bin/yarn
npm: 6.14.4 - /usr/local/bin/npm
Browsers:
Chrome: 84.0.4147.105
Edge: Not Found
Firefox: Not Found
Safari: 13.1.2
npmPackages:
react: Not Found
react-dom: Not Found
react-scripts: Not Found
npmGlobalPackages:
create-react-app: Not Found
Steps to reproduce
npx create-react-app@next --scripts-version=@next --template=cra-template@next my-js-app
cd my-js-app
yarn set version berry
yarn set version latest
yarn install
yarn start
Expected behavior
App is served without problems.
Actual behavior
App throws an error.
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 4
- Comments: 17 (6 by maintainers)
So crazy story. Do
npm install
and then try starting it again. To me, that seems like a bug in Yarn2Bump - I think I’ll proceed on making a PR to push further discussions.
Maintainer of the fast refresh plugin here.
Technically speaking, it is always unsound to import something that you haven’t listed in your dependencies. Through hoisting it might work (for example in CRA v3
@babel/core
), but importing stuff that was never listed means the installation chain is prone to breakage.Yes, that is the case. We had to do that (kinda like how
@babel/runtime
works) because there is no other way for us to inject a globally available dependency throughout the bundle without leaking implementation into the actual global scope.To me I think the best resolution to this problem would be to list
react-refresh
as an installeddependency
by default in the templates, and also apeerDependency
ofreact-scripts
. While this kinda breaks the “single-dependency” promise, given that we’re already shipping RTL as a default in the templates, I don’t see how this is problematic.I can PR this change (or actually, anyone can do it) - but I’ll wait for the maintainers’ point of view on this before progressing.
When I add
react-refresh
todependencies
, it still doesn’t work. I get a new error,Failed to load config "react-app" to extend from.
Edit: Possible solution! run
yarn add react-refresh eslint-config-react-app
I just setup create-react-app using Yarn 2 today and I got this error. What is the proposed resolution for this?
@volodymyr-kushnir Or you know, enabling the
node-modules
linker in V2 usingyarn config set nodeLinker node-modules
or simply adding the missing dependencies usingyarn add react-refresh eslint-config-react-app
@eddiemonge That’s not a solution and it’s not an issue in Yarn 2’s PnP mode, they are simply undeclared dependencies
@pmmmwh It would be better for the plugin to resolve the import before injecting it, that way it works no matter what, PR at https://github.com/pmmmwh/react-refresh-webpack-plugin/pull/230
Add it as a dependency until https://github.com/facebook/create-react-app/pull/9872 is merged
Hitting this error now. Does
react-refresh
go intodevDependencies
, ordependencies
?@eddiemonge it works with npm and it works with Yarn 1.22.4 as well, because both of them use node_modules. Yarn 2 on the other hand keeps packages in .yarn/cache and complains if packages try to require modules they didn’t list as their dependencies, because, even though it has
react-refresh
hoisted to the top, modules must explicitly list versions of dependencies they need. What I’m saying is that create-react-app 4 won’t work with PnP in Yarn 2 unlessreact-refresh
is also listed as a dependency 🤷🏼♂️. All other setups would work fine. I’m just curious if anybody has an elegant solution for this specific use case.