core: [nextjs-mf v7] SWC error: `Cannot read properties of undefined (reading 'call')`
As discussed a few days ago on Twitter with @ScriptedAlchemy, an error happens when using the nextjs-mf plugin.
Sometimes it works when accessing directly the host (http://localhost:3001), but then if I access the remote in the browser (http://localhost:3000), error appears in host. It’s really erratic and I don’t understand what’s going on here.
One thing is sure, the SWC compiler is doing some weird stuff because if I switch to Babel in the remote it works correctly.
SWC may transform either the remoteEntry.js or the exposed module (or both?). And there’s no way to tell Next to not doing this right now.
Error
TypeError: Cannot read properties of undefined (reading 'call')
at options.factory (host/.next/static/chunks/remoteEntry.js (780:31))
Context
- nextjs-mf v7.0.0 without delegated modules
- react v18.2.0
- next v13.4.8 with SWC compiler (enabled by default today in Next.js) and /pages dir
- uses browserslist queries in remote (
"cover 90% in FR", "last 3 versions", "not dead", "maintained node versions"
) - either with
import from
or React Lazy + React Suspense - mac OS (M1) / node.js v18.16.0
How to reproduce
Apps were generated with Next CLI, so it’s really a basic config.
- Clone this repo: https://github.com/yspychala/nextjsmf-v7-swc-debug
npm i
in /host and /remote directoriesnpm run dev
in /host and /remote- Go to
http://localhost:3000
(remote) andhttp://localhost:3001
(host) - See error in host
If we turn off SWC if favor of Babel in remote, it works again:
// Create .babelrc in /remote and restart servers
{
"presets": ["next/babel"]
}
About this issue
- Original URL
- State: closed
- Created a year ago
- Reactions: 7
- Comments: 50 (24 by maintainers)
I don’t know whether it is related but I am getting this after the update to nextjs-mf-7.0.2:
skipSharingNextInternals
- im not sure thats still implemented.If microfrontend or module federation is important to you, then next.js is absolutely not the framework to use. modernjs however is built for federation and is what i primarily focus on.
Its next release includes SSR + (HMR, TS) support too, so no need to use next.
Next.js isnt built for this
@yspychala here’s the workaround:
Create a file called
delegate-module.js
somewhere near your next/webpack config and paste the following (it’s just copied from the plugin source code but removes imports):`delegate-module.js`
Then update your remotes config like this:
This should fix the remainder of the errors, but let me know if it still pops up, i might have some other ideas.
Okay long standing issue, through the unification process with bytedance, bore some fruits for next.js
https://github.com/module-federation/universe/pull/1268 resolves or should resolve any and all module consumption related problems. Thanks to https://github.com/module-federation/universe/pull/1224 - I now have absolute control
Specifically this: https://github.com/module-federation/universe/pull/1433
Should resolve “call of undefined” and “eager consumption” errors, particularly eager consumption, which has been the root cause of most of the Problems with Next.js - import() is no longer required for module federation to work
Is there a version that we can test these changes on? @ScriptedAlchemy if possible please confirm which import path is now recommended for next.js (script injection? dynamic? react.lazy?)
Still getting this
updateHash
error occasionally, no clear way to reproduce it consistently. A server reboot usually makes it go away. Did anyone manage to fix it?@yspychala excellent, thank you - ill take a look hopefully this evening. @beratbayram i was helping a user debug this morning and we were able to incrementally get updateHash error. Not sure if related either, but they also have call of undefined - but remoteEntry never loaded and webpack crash immediately. Ill investigate if i can reproduce on a public repo.
If you have issues - stick on v6 so you are not blocked. This was a major rewrite and sadly testing can only flush out so many use cases, few users use beta but we had a few weeks of no issues reported. So the only way to flush out additional cases was to publish v7 and get more user feedback.
Just some further info. I tried some configs and from the error call stack, it seems Webpack runtime script is trying to load the shared module
react
from theremote
scope regardless that I have already declared a compatibleshared
deps inhost
webpack config. The actualreact
module is not supposed to be loaded fromremote
and the actualreact
files should not exist inremote
because the@module-federation/nextjs-mf
internally has defaultshared
:@ScriptedAlchemy Thank you for update. I did update MF to v7.0.6, seems this error
"Cannot read properties of undefined (reading 'call')"
fixed but faced other issue with next build,dev mode
works perfectly fine.okay i think i have found a solution to this: https://github.com/module-federation/universe/pull/1149
Not sure if this was mentioned anywhere in this thread but for me this only happens when I go directly to a route with a federated module. If I load into the host app where there is no remoteEntry and then navigate to a page with module federation, the issue doesn’t happen. Once it crashes though, nothing loads.
EDIT: Looks like once a federated module has been loaded, any type of hard refreshing crashes the app.
hey @ScriptedAlchemy, faced similar issue Basically at v6.7.1 all work as expected without any errors, soon when version updated to v7.0.0 this issues upper.
Builded code
use
next
tag on nom for the v8 prerelease. https://github.com/module-federation/universe/issues/1530Hi @ScriptedAlchemy can I confirm that this is fixed in the following versions please?
If you install node utilities and nextmf seperately. It will work though. I’m glad it looks to be headed in the right direction, thanks for the feedback
@ScriptedAlchemy i just ran 8.0.0 locally and the “Cannot read properties of undefined (reading ‘updateHash’)” issue seems to be resolved. The library is also handling unavailable remotes better with a log in console instead of complete failure. Will report back if anything unexpected comes up. next/error import is still not possible.
@simonbergstrom i did managed via custom delegate Basically as this comment https://github.com/module-federation/universe/issues/1102#issuecomment-1632909792
hey @ScriptedAlchemy, any news in progress ? mainly
node_modules/@module-federation/utilities/src/utils/importDelegatedModule.js
Self-reference dependency has unused export name: This should not happenhttps://github.com/module-federation/universe/issues/1102#issuecomment-1642794271
FYI I did use suggest from https://github.com/module-federation/universe/issues/1102#issuecomment-1632909792 and used internal delegated module and seems build fixed, all work
next/image
resolved in https://github.com/module-federation/universe/pull/1149 as well.thanks, ill take a peek soon as possible