Resolve module (mjs) incorrectly when using Module Federation Plugin
See original GitHub issueBug report
What is the current behavior?
When using swr/infinite with swr>1.1.2 dependency and sharing it with Module Federation (SharePlugin)
new webpack.container.ModuleFederationPlugin({
shared: ['swr'],
}),
We encountered this error:

If the current behavior is a bug, please provide the steps to reproduce.
I created this repo that reproduces the issue: https://github.com/tzachbon/swr-mf-error
All you have to do is to use SWR >= 1.2.0 and add it to the shared array in the Module Federation Plugin. In the change log here: https://github.com/vercel/swr/compare/1.1.2...1.2.0 I found a few things that might be the reason.
In my code, I try to import swr/infinite, which now has exports field in its package json:
"exports": "./dist/index.mjs",
In the swr package json, the exports field now has a filename that ends with mjsextension:
"./infinite": {
"import": "./infinite/dist/index.mjs",
"module": "./infinite/dist/index.esm.js",
"require": "./infinite/dist/index.js",
"types": "./infinite/dist/infinite/index.d.ts"
},
When I manually changed the filename to .js it worked.
What is the expected behavior?
To resolve it like .js
Other relevant information: webpack version: 5.74.0 Node.js version: 16.15.0 Operating System: MacBook Pro (16-inch, 2021) Additional tools: swr: 1.3.0
Issue Analytics
- State:
- Created a year ago
- Reactions:5
- Comments:14 (3 by maintainers)

Top Related StackOverflow Question
You can skip this issue by also sharing any submodule of swr changing this:
to this
I’ve been able to figure out that the issue is in the detection of exportsType of a shared module when passing through from another shared module, it comes back as
default-with-namedinstead ofdynamic, which then means thatinteropDefaultAccessUsednever gets set to true, andcompatGetDefaultExportnever gets added to the build. What I can’t figure out is why it breaks only when coming in through another shared module. If imported as a first-level shared module, this doesn’t happen, the file is correctly typed as dynamic.