You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Resolve symlinks using Node resolution mechanism #3883
Let's forget about the issue about compiling source code for a second. I feel like that can be solved by #1333 and thus is not the most important one here.
The important part here is that the resolution should match Node algorithm so that our webpack setup matches our test setup. I thought that was the case, but I was wrong.
Consider this structure:
my-comp
index.js // already compiled
package.json // react is a peer dependency
my-app
node_modules
my-comp // symlink to my-comp folder
It turns out that if my-comp doesn't declare a dependency on react, it can find React in the browser builds but not in tests.
We probably missed it because if my-compdoes explicitly depend on React (e.g. as a devDependency), the test passes (and in the browser we just get two Reacts).
After more digging, the reason "jest already worked correctly" in #1359 per #1359 (comment) was because Jest was not respecting Node resolution. Since I fixed Jest in jestjs/jest#4761, that integration has regressed. But again, it was broken in the first place because it didn't match how Node resolves modules.
I found a workaround that I use in my projects. You can run a watch process to compile your shared library and use it as symlink in your project. Here is my gist.
Is there a scenario where someone would prefer having dependencies read from linked node_modules instead from CRA-based project's node_modules? Modifying webpack.config.js to have { resolve: { symlinks: false } } fixes the "two react" problem.
If there is no scenario, then maybe CRA should have the value, from above, hardcoded?
Reacted by Sebastian "Sebbie" Silbermann, Eric Anderson, Julien Vallini and Andreas Kohn
Activity
gaearon commentedon Jan 21, 2018
I think the correct fix would be to:
npm linkreally wantgaearon commentedon Jan 21, 2018
Even more interesting. Apparently Jest itself doesn't quite use Node module resolution.
In fact I was the one who changed Jest from
--preserve-symlinksbehavior to normal behavior 😅 jestjs/jest#4761It seems like maybe we should support both? I believe webpack has a flag for it. Maybe Jest could have a flag too.
gaearon commentedon Jan 21, 2018
While this is fresh in my mind, I immortalized this dilemma in a tweet.
https://mobile.twitter.com/dan_abramov/status/955061849648779264
Hope this is helpful to whoever ends up reading this to consider fixes (me?)
gaearon commentedon Jan 21, 2018
Filed an issue in Jest.
jestjs/jest#5356
gaearon commentedon Jan 21, 2018
@bradfordlemley Do you have thoughts on this? You probably spent more thinking about symlinks than most of us :-)
gaearon commentedon Jan 21, 2018
After more digging, the reason "jest already worked correctly" in #1359 per #1359 (comment) was because Jest was not respecting Node resolution. Since I fixed Jest in jestjs/jest#4761, that integration has regressed. But again, it was broken in the first place because it didn't match how Node resolves modules.
1st commentedon Mar 27, 2018
Is it somehow related to #3547?
1st commentedon Mar 27, 2018
I found a workaround that I use in my projects. You can run a watch process to compile your shared library and use it as symlink in your project. Here is my gist.
conartist6 commentedon Jun 16, 2018
Any progress on this? I'm game to help if there's work to be done.
omerdn1 commentedon Sep 11, 2018
Any progress or a workaround for this that doesn't require running a watcher compiler?
ianqueue commentedon Mar 1, 2019
Any progress on this?
pietmichal commentedon Mar 7, 2019
Is there a scenario where someone would prefer having dependencies read from linked node_modules instead from CRA-based project's node_modules? Modifying
webpack.config.jsto have{ resolve: { symlinks: false } }fixes the "two react" problem.If there is no scenario, then maybe CRA should have the value, from above, hardcoded?
tannerlinsley commentedon Jul 3, 2019
I just encountered this and after many many attempts to get around it, I ended up installing
rescriptsand doing this:Leaving here for posterity.
temaivanoff commentedon Feb 4, 2020
Up
ianschmitz commentedon Feb 6, 2020
I believe #7993 will fix this.
PeledYuval commentedon Aug 4, 2020
Over at Vim we've arrived at a decent working solution using craco.
I've elaborated here: #3547 (comment)