At work we have a shell app that loads a bunch of micro-frontends through Module Federation, and the shell is moving to React 19 while one of the remotes, owned by another team and loaded by a few other hosts as well, stays on React 18 for a while. Getting the two onto the same page took two changes, not one. The obvious fix turned out to be only half of it, and the other half lives in a part of Module Federation’s sharing config that I had read about plenty of times but never really thought through.
Two Reacts can’t share a tree
Before the upgrade the shell rendered the remote’s exported component directly in its own tree, which is the usual way to consume a federated React component. With React 19 in the host that stopped working as soon as the remote’s chunk loaded:
TypeError: Cannot read properties of undefined (reading 'ReactCurrentOwner')
React 18’s bundled jsx-runtime reaches into React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentOwner, and it does that at the top of the module, so with 19 underneath it the remote falls over as soon as that module is evaluated, before it has rendered anything. Even a remote that loads with its own React 18 can’t hand its elements to the host, because React 19 marks elements with a different $$typeof and throws on the ones 18 made. Elements, hooks and context from one major aren’t valid in the other, which means the remote can’t be a component in the host’s tree at all.
The way around that is to stop passing React things across the boundary. The host gives the remote a DOM node, and the remote creates a root in it with its own React:
// mount.tsx, exposed by the remote
import { createRoot } from 'react-dom/client';
export const mount = (container: HTMLElement, props: AppProps) => {
const root = createRoot(container);
root.render(<App {...props} />);
return () => root.unmount();
};
On the host side the loader renders an empty div, calls mount(container, props) once the module has loaded, and keeps the returned function to call when the host unmounts, so the only things crossing over are a DOM node, a plain props object and a function.
If you try this, the remote’s
mountexpose has to ship before the host that asks for it, because a React 19 host has no working fallback to the old component export.
The share scope
That can still crash with the same ReactCurrentOwner error, and the reason is in how the remote shares React:
shared: {
react: { singleton: true, requiredVersion: '^18.3.1' },
'react-dom': { singleton: true, requiredVersion: '^18.3.1' },
}
It reads like “give me React 18, and only one copy of it”, but without strictVersion the requiredVersion is only a preference. If a higher React is already registered in the same share scope, the runtime logs a warning and hands that one over, so the remote’s own createRoot ends up running against React 19 and we’re back where we started. Every remote registered with registerRemotes goes into the default scope unless told otherwise, and any other remote that shares React 19 puts it there.
The sibling remote I was testing next to did exactly that, so my first fix stopped it from sharing React, and a manual run through the app looked fine. In review it turned out that the run only passed because the React 18 remote happened to load first. A third remote, from yet another team, also shares React 19, so opening its page before the React 18 one would put React 19 in the scope again. I found that one by reading the configs, not by hitting it, but the crash itself was easy to stage once I knew to look for it. Fixing a share-scope leak by changing which other remotes share React only lasts until the next remote that shares it, and I dropped that change once the real fix was in.
Changing the remote’s own config wasn’t much better. strictVersion: true turns the crash into a clean load error, which is nicer to debug but still a page that doesn’t load, and that config also serves the other hosts that load the remote, which are still on React 18 and doing fine.
What fixed it is giving the DOM-mounted remote a share scope of its own when the host registers it:
import { registerRemotes } from '@module-federation/enhanced/runtime';
// only for remotes that get a DOM node; the rest are registered without shareScope
registerRemotes([{ name, entry, shareScope: name }], { force: true });
When the runtime meets a scope name it hasn’t seen before, createRemoteEntryInitOptions creates an empty shareScopeMap[shareScope] for it, so the remote starts with nothing to pick up and resolves the React 18 it bundled, while the remotes that still render as components keep sharing default with the host as before. With React 19 put into default on purpose before the remote loaded, it still came up on its own React 18.
That isolation has a price. An empty scope means the remote loads its own React and React DOM, and everything else it marks as shared comes from its own bundle too, so nothing is shared with the host anymore. That’s what you want for a DOM-mounted remote, and here it costs nothing new, since the order that used to work already loaded both Reacts. But if you have several remotes on the old major, give them one scope between them (react18, say) rather than one each, or each of them downloads its own copy.
The scope only applies when the remote is first registered, or re-registered with force. Reading the runtime, a container that something else already initialized keeps the scope it got back then, so the option has to go through every path that registers the remote, including a container that’s already on window and a forced re-registration, not only the first load. Each of those paths deserves its own test, because a missed one brings the bug back only on that path.
The mount lifecycle
Once the remote owns its root, the host loader has more cases to handle than it had with a lazy component, where React and Suspense did most of that work:
mount()can return a promise (anything with a callablethen, not only a nativePromise), and if it never settles the container stays blank with no error. The loader gives it a timer of its own, started once the module has loaded, so a slow load and a hung mount add up before you see an error.- An async
mount()can resolve after the host has unmounted (the user navigated away), and the late unmount function still has to be called, or the remote’s root stays alive on a detached node. - In development, StrictMode runs the effect, cleans it up and runs it again, so an async
mount()gets called twice on the same container while the first call is still pending. The remote’smounthas to cope with that, for example by doing its async work before it callscreateRoot. - A
mount()that returns anything other than a function breaks the contract, and the host shows its error state, since it has no handle on a root the remote created and didn’t hand back. - Props are read once, at mount, so a changed prop doesn’t reach the remote unless it’s remounted, or unless the remote exposes something like an
update(props)that callsroot.renderagain. That includes things like an auth token, which the remote has to get some other way if it can change while the page is open.
Roughly what the effect ended up as. domMount is state set once when the module finishes loading, so the effect runs once per load; build it inline during render and the remote remounts on every render.
useEffect(() => {
const container = containerRef.current;
if (!domMount || !container) return undefined;
const { remote, timeout } = domMount;
let cancelled = false;
let unmount: (() => void) | undefined;
let timer: ReturnType<typeof setTimeout> | undefined;
const fail = (err: unknown) => {
if (cancelled) return;
showError(err);
};
const settle = (result: unknown) => {
if (cancelled) {
if (typeof result === 'function') safeUnmount(result);
return;
}
if (typeof result !== 'function') {
throw new Error('mount() did not return an unmount function');
}
unmount = result;
};
try {
const result = remote.mount(container, propsRef.current);
if (isPromiseLike(result)) {
timer = setTimeout(() => {
fail(new Error(`mount() timed out after ${timeout}ms`));
cancelled = true;
}, timeout);
Promise.resolve(result).then(settle).catch(fail).finally(() => clearTimeout(timer));
} else {
settle(result);
}
} catch (err) {
fail(err);
}
return () => {
cancelled = true;
clearTimeout(timer);
if (unmount) safeUnmount(unmount);
};
}, [domMount]);
safeUnmount is a try/catch around the remote’s cleanup that logs the error, because a remote that throws while unmounting shouldn’t break the host’s own unmount.
Summary
When a host and a remote are on different React majors, the remote has to be mounted into a DOM node with its own root and also loaded under a share scope of its own. You need both: without the first, React 19 throws on the elements the remote hands it, and without the second, the remote crashes as soon as its chunk loads whenever another remote got React 19 into the default scope first. A singleton with a requiredVersion and no strictVersion is a preference rather than a guarantee, so when a bug comes and goes with the order in which remotes load, the share scope is the first place I’d look.