module-federation / module-federation-examples

Implementation examples of module federation , by the creators of module federation
https://module-federation.io/
MIT License
5.54k stars 1.73k forks source link

Module federation is working with npm run dev but not with npm run build then npm run start (Container initialization failed as it has already been initialized with a different share scope) #3474

Open AmineAyachi opened 9 months ago

AmineAyachi commented 9 months ago

Hello i created a new next js application with this configs: package.json:

{
  "name": "template-host-app",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start",
    "lint": "next lint"
  },
  "dependencies": {
    "@module-federation/nextjs-mf": "^7.0.8",
    "next": "13.5.6",
    "react": "^18.2.0",
    "react-dom": "^18.2.0"
  },
  "devDependencies": {
    "@types/node": "^20",
    "@types/react": "^18.2.45",
    "@types/react-dom": "^18",
    "eslint": "^8",
    "eslint-config-next": "14.0.4",
    "typescript": "^5.3.3"
  }
}

tsconfig.json :

{
  "compilerOptions": {
    "target": "es5",
    "lib": ["dom", "dom.iterable", "esnext"],
    "allowJs": true,
    "skipLibCheck": true,
    "strict": true,
    "noEmit": true,
    "esModuleInterop": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "jsx": "preserve",
    "incremental": true,
    "paths": {
      "@/*": ["./*"]
    }
  },
  "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
  "exclude": ["node_modules"]
}

next.config.js:

/** @type {import('next').NextConfig} */

const { NextFederationPlugin } = require('@module-federation/nextjs-mf');

 const remotes = (isServer) => {
   const location = isServer ? 'ssr' : 'chunks';
   return {
     // specify remotes
     remote: `remote@http://localhost:3001/_next/static/${location}/remoteEntry.js`,
   };
 }

 const nextConfig = {
   reactStrictMode: true,
   webpack(config, { isServer }) {

    config.resolve.fallback = {
      fs: false,
    };

     config.plugins.push(
       new NextFederationPlugin({
         name: 'host',
         filename: 'static/chunks/remoteEntry.js',
         remotes: remotes(isServer),
         exposes: {
           // Host app also can expose modules
         }
       })
     );

     return config;
   },
 }

module.exports = nextConfig

When i run npm run dev , everthing works but when i run npm run build then npm run start i got the application runing but it expose a bad remoteEntry.js file : here is the file : https://docs.google.com/document/d/1x5JYZRq-1-Hzx1FXzKLShEGk49NYEJ5Vz7IiWRzowBg/edit?usp=sharing here is the url i am using for remoteEntry.js : http://localhost:3000/_next/static/chunks/remoteEntry.js

IkhyeonKim commented 7 months ago

@AmineAyachi Hi! I encountered the same situation, have you figured this out?

AmineAyachi commented 7 months ago

hi its a problem with the package i used the last version but i still have issues take a look on that : https://github.com/module-federation/universe/issues/1773#issuecomment-1901948791