reactjsapollo-clientwebpack-5micro-frontendwebpack-module-federation

Module Federation, React, and Apollo 3 warnings


I'm building an app with micro-frontends using webpack 5's module federation plugin. Everything was fine until I started adding react hooks into my remote app. At that point I received errors about "invalid usage of hooks", i.e. I discovered I had TWO versions of react loaded, one from the remote and one from the app consuming the remote.

That problem was solved by adding a shared key to the ModuleFederationPlugin section of my webpack config that marked React as a singleton. Now everything compiles and seems to run just fine.

However, the webpack compiler is throwing some annoying warnings at me now. Its saying:

No required version specified and unable to automatically determine one. Unable to find required version for "react" in description file (/Users/myComputer/Development/myapp/node_modules/@apollo/client/react/context/package.json). It need to be in dependencies, devDependencies or peerDependencies.

Here is what my webpack config (in the remote) looks like currently:

const HtmlWebpackPlugin = require('html-webpack-plugin')
const ModuleFederationPlugin = require('webpack/lib/container/ModuleFederationPlugin')

const deps = require('./package.json').dependencies

module.exports = {
  mode: 'development',
  devServer: { port: 3001 },
  entry: './src/index.tsx',
  output: {
    path: __dirname + '/dist/',
  },
  module: {
    rules: [
      {
        test: /\.(ts|tsx)$/,
        exclude: /node_modules/,
        resolve: {
          extensions: ['.ts', '.tsx', '.js', '.json'],
        },
        use: 'ts-loader',
      },
    ]
  },
  devtool: 'source-map',
  plugins: [
    new ModuleFederationPlugin(
      {
        name: 'myRemote',
        filename: 'remoteEntry.js',
        exposes: {
          './App':
            './src/App/App.tsx',
        },
        shared: {
          'react': {
            singleton: true,
            version: deps['react'],
          },
          'react-dom': {
            singleton: true,
            version: deps['react-dom'],
          },
        },
      }
    ),
    new HtmlWebpackPlugin({
      template:
        './index.html',
    })
  ]
}

The consuming app's webpack config is almost the same, especially the shared section (there are some slight differences in that it declares the remotes).

What would be the way to tell webpack that the apollo package will be getting its react dependency from somewhere else? Or if thats not the right thing to tell webpack, what is and how can I get rid of these warnings?


Solution

  • Fixed my own problem by changing the key version to requiredVersion