core: Error: Shared module react doesn't exist in shared scope default

Getting this error when trying to use the new app router with @module-federation/nextjs-mf

Server Error
Error: Shared module react doesn't exist in shared scope default

This error happened while generating the page. Any console logs will be displayed in the terminal window.
Call Stack
ensureExistence
file:///Users/georgiosntounis/Documents/test-projects/next-mfe/.next/server/webpack-runtime.js (881:68)

Here is what I have in my next.config.js file

const NextFederationPlugin = require("@module-federation/nextjs-mf");

const remotes = () => {
  return {
    "my_components": `my_components@http://localhost:8082/remoteEntry.1.0.0.js`,
  };
};

const nextConfig = {
  webpack(config, { isServer }) {
      config.plugins.push(
        new NextFederationPlugin({
          name: "host",
          filename: "static/chunks/remoteEntry.js",
          remotes: remotes(),
          shared: {},
        })
      );
    return config;
  },
};

module.exports = nextConfig;

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Reactions: 7
  • Comments: 37 (11 by maintainers)

Most upvoted comments

Hi @ScriptedAlchemy You don’t need any complex examples. https://github.com/Muskos/nextjs-mf-app-folder Here is just the Nextjs app with @module-federation/nextjs-mf plugin and one page in the app folder. You can reproduce it here.

yes RSC will get support eventually. With v7 now done, if the community wants to help (i know a few were interested in trying a few things) - im happy for extra hands.

First step i need is a PR of my repo using and breaking with app dir, then ill see what needs to be done to make it not error haha.

AFAIK, App dir isn’t supported yet. But I read it was planed after the release of v7.

Hello, I’m facing the same issue

** Server Error** Error: Shared module react-dom doesn’t exist in shared scope default This error occurred while generating the page. Any console logs will be displayed in the terminal window.

I’m using Next.js 13, is there any solution available?

"next": "13.5.6"
"react": "^18.2.0"
"react-dom": "^18.2.0"

Something very important is that i am using the “pages” directory instead of the new “app” directory! This might be also part of why this setup is working.

This is my entire next.config.js (I am also using experimental imports from Framer)

/** @type {import('next').NextConfig} */
const { NextFederationPlugin } = require("@module-federation/nextjs-mf");
const path = require("path");
const nextConfig = {
  reactStrictMode: true,
  distDir: "build",
  webpack(config, options) {
    if (!options.isServer) {
      config.resolve.alias["react/jsx-runtime"] = path.join(
        __dirname,
        "/node_modules/react/jsx-runtime.js"
      );
      config.resolve.alias["react/jsx-dev-runtime"] = path.join(
        __dirname,
        "/node_modules/react/jsx-dev-runtime.js"
      );
      config.resolve.alias["react"] = path.join(
        __dirname,
        "/node_modules/react/index.js"
      );

      config.plugins.push(
        new NextFederationPlugin({
          name: "DreamComponents",
          filename: "static/chunks/remoteEntry.js",
          exposes: {
            "./components": "./src/components/index.ts",
          },
          shared: {},
        })
      );
    }

    return config;
  },
  experimental: {
    urlImports: [
      "https://framer.com/m/",
      "https://framerusercontent.com/",
      "https://ga.jspm.io/",
      "https://jspm.dev/",
      "https://rive.app/",
      "https://boosters.flowbase.co/",
    ],
  },
};

module.exports = nextConfig;

I hope this helps. @rahimimo

Does this configuration works with ssr? I mean when option.isServer true or when we need module federation with ssr?

"next": "13.5.6"
"react": "^18.2.0"
"react-dom": "^18.2.0"

Something very important is that i am using the “pages” directory instead of the new “app” directory! This might be also part of why this setup is working.

This is my entire next.config.js (I am also using experimental imports from Framer)

/** @type {import('next').NextConfig} */
const { NextFederationPlugin } = require("@module-federation/nextjs-mf");
const path = require("path");
const nextConfig = {
  reactStrictMode: true,
  distDir: "build",
  webpack(config, options) {
    if (!options.isServer) {
      config.resolve.alias["react/jsx-runtime"] = path.join(
        __dirname,
        "/node_modules/react/jsx-runtime.js"
      );
      config.resolve.alias["react/jsx-dev-runtime"] = path.join(
        __dirname,
        "/node_modules/react/jsx-dev-runtime.js"
      );
      config.resolve.alias["react"] = path.join(
        __dirname,
        "/node_modules/react/index.js"
      );

      config.plugins.push(
        new NextFederationPlugin({
          name: "DreamComponents",
          filename: "static/chunks/remoteEntry.js",
          exposes: {
            "./components": "./src/components/index.ts",
          },
          shared: {},
        })
      );
    }

    return config;
  },
  experimental: {
    urlImports: [
      "https://framer.com/m/",
      "https://framerusercontent.com/",
      "https://ga.jspm.io/",
      "https://jspm.dev/",
      "https://rive.app/",
      "https://boosters.flowbase.co/",
    ],
  },
};

module.exports = nextConfig;

I hope this helps. @rahimimo

use “canary” tags

Open to PRs for this longstanding issue. Modern.js will support RSC before Next.js due to its open-source priority. It’s easier to implement in Modern.js, which has an unparalleled Developer Experience (DX) for Micro Frontends (MFE). The focus is on releasing the internal version to the open-source community, offering a meta-framework for microfrontends and horizontal scaling—something unmatched in my consulting experience with other companies’ in-house MFE or MFP efforts.

Planning a fork of Module Federation to release an enhanced version with broad support for TypeScript (TS), Hot Module Replacement (HMR), Server-Side Rendering (SSR), dynamic loading, automatic route stitching, middleware, and more. For details, see issue #1170.

Challenges with Next.js are significant; it’s not designed for module federation. Enhancements are often complex or nearly impossible. For an in-depth understanding, refer to the InvertedContainerRuntimeModule on GitHub.

“Container Inversion” involves a complete overhaul of webpack’s JSONP and dependency management systems. Despite its complexity, it has solved other architectural issues, especially with circular or nested remotes. This will be integrated into V1.5 of the forked federation. RSPack.dev plans to implement V1.5 in Rust by Q1, offering native support for Module Federation. Concurrently, I’ll work with the Modern.js team to reorganize their internal fork and decouple it from our internal deployment infrastructure.

There’s overlap between the needs of Modern.js and Next.js. Bytedance has solved many issues at scale, and my recent Next.js workarounds have led to beneficial patterns. These will be integrated into my module-federation ecosystem with web-infra-dev.

I’m currently integrating the node federation specification for Modern.js as we perfect SSR and backend integrations. Plans are underway to use federation in CI, build management, and Infrastructure as Code, necessitating a memory-efficient server solution.

Timeline predictions: RSC support in Next.js is at least 6 months away. If urgently needed, our consulting/pro services branch can fund it. Modern.js will likely have RSC Federation support 2-3 weeks after RSC itself is supported. Unless a breakthrough or business case allows for resource reallocation, the timeline remains uncertain. Expect more insights in 1-2 months.

URLs:

I try to run this project: https://github.com/Muskos/nextjs-mf-app-folder same error shows: Error: Shared module react doesn't exist in shared scope default

Have you tested it yourself?

Hi @ScriptedAlchemy You don’t need any complex examples. https://github.com/Muskos/nextjs-mf-app-folder Here is just the Nextjs app with @module-federation/nextjs-mf plugin and one page in the app folder. You can reproduce it here.

@ScriptedAlchemy I was just working on this… you can use my repo, created a branch which shows the issue clearly : https://github.com/nameofname/next-module-federation/tree/nameofname/app-router-error/mfe2

Inside the dir mfe2/src there are 2 directories, app and pages-bak … both are bare bones examples of app router and pages router. I tested locally and if I run npm run dev from within mfe2 then I will encounter the error.

If I mv app app-bak && mv pages-bak pages then cd.. && npm run dev then the error will go away.

nameof ~/github/next-module-federation/mfe2 (nameofname/app-router-error) # npm run dev

> mfe2@1.0.0 dev
> rm -rf .next; next dev -p 3002

- ready started server on 0.0.0.0:3002, url: http://localhost:3002
[nextjs-mf]: automaticAsyncBoundary is deprecated
[nextjs-mf]: automaticAsyncBoundary is deprecated
(node:18010) [DEP_WEBPACK_MODULE_ID] DeprecationWarning: Module.id: Use new ChunkGraph API
(Use `node --trace-deprecation ...` to show where the warning was created)
- event compiled client and server successfully in 240 ms (107 modules)
- wait compiling...
- event compiled client and server successfully in 112 ms (107 modules)
- wait compiling /page (client and server)...
- event compiled client and server successfully in 1677 ms (478 modules)
- error Error: Shared module react doesn't exist in shared scope default
    at ensureExistence (/Users/nameof/github/next-module-federation/mfe2/.next/server/webpack-runtime.js:800:68)
    at /Users/nameof/github/next-module-federation/mfe2/.next/server/webpack-runtime.js:878:13
    at /Users/nameof/github/next-module-federation/mfe2/.next/server/webpack-runtime.js:863:20
    at webpack/sharing/consume/default/react (/Users/nameof/github/next-module-federation/mfe2/.next/server/webpack-runtime.js:915:74)
    at /Users/nameof/github/next-module-federation/mfe2/.next/server/webpack-runtime.js:959:56
    at Array.forEach (<anonymous>)
    at __webpack_require__.getEagerSharedForChunkId (/Users/nameof/github/next-module-federation/mfe2/.next/server/webpack-runtime.js:928:36)
    at /Users/nameof/github/next-module-federation/mfe2/.next/server/app/page.js:1869:72
    at Object.<anonymous> (/Users/nameof/github/next-module-federation/mfe2/.next/server/app/page.js:1882:3)
    at Module._compile (node:internal/modules/cjs/loader:1149:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1203:10)
    at Module.load (node:internal/modules/cjs/loader:1027:32)
    at Module._load (node:internal/modules/cjs/loader:868:12)
    at Module.require (node:internal/modules/cjs/loader:1051:19)
    at require (node:internal/modules/cjs/helpers:103:18)
    at requirePage (/Users/nameof/github/next-module-federation/mfe2/node_modules/next/dist/server/require.js:156:12)
    at /Users/nameof/github/next-module-federation/mfe2/node_modules/next/dist/server/load-components.js:71:84
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async loadComponentsImpl (/Users/nameof/github/next-module-federation/mfe2/node_modules/next/dist/server/load-components.js:71:26)
    at async DevServer.findPageComponentsImpl (/Users/nameof/github/next-module-federation/mfe2/node_modules/next/dist/server/next-server.js:772:36) {
  digest: undefined
}
- wait compiling /_error (client and server)...
- event compiled client and server successfully in 478 ms (573 modules)

I’m also getting same error as that @gdunix is facing. Did anyone found solution? @ScriptedAlchemy @yspychala @gpatatoukas