webpacknext.jswarningswebpack-5swr

Shared module warning when upgrading swr from version 1 to version 2 in a NextJS app


I am upgrading my swr package from my current version 1 to the latest version 2. Below is my package.json. I am using React 18, NextJS 12, and Webpack 5. The project integrates ModuleFederationPlugin.

{
  "scripts": {...},
  "dependencies": {
    ...
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "next": "12.2.2",
    "swr": "^2.2.2",
    "webpack": "^5.78.0",
    ...
  }
  "devDependencies": {...}
}

Below is my next.config.js shared swr item for the ModuleFederationPlugin:

// eslint-disable-next-line import/no-extraneous-dependencies
const ModuleFederationPlugin = require('webpack/lib/container/ModuleFederationPlugin');
const deps = require('./package.json').dependencies;

module.exports = {
  basePath: process.env.BASE_PATH,
  reactStrictMode: true,
  env: {...},
  webpack(config) {
    config.module.rules.push({
      test: /\.svg$/,
      use: ['@svgr/webpack'],
    });

    config.plugins.push(
      new ModuleFederationPlugin({
        name: 'someName',
        exposes: {},
        shared: [
          {
            react: {
              singleton: true,
              requiredVersion: deps.react,
              eager: true,
            },
            'react-dom': {
              singleton: true,
              requiredVersion: deps['react-dom'],
              eager: true,
            },
            'swr/': {
              // requiredVersion: deps.swr,
              requiredVersion: '^2.2.2',
              eager: true,
            },
            ...
          },
        ],
      }),
    );

    return config;
  },
};

When I upgrade my swr package, below is the warning that I get:

warn  - shared module /Users/nextjs-app/node_modules/swr/_internal/dist/index.mjs -> /Users/nextjs-app/node_modules/swr/_internal/dist/index.mjs
No version specified and unable to automatically determine one. No version in description file (usually package.json). Add version to description file /Users/nextjs-app/node_modules/swr/_internal/package.json, or manually specify version in shared config.

So, I changed the shared path for swr in the ModuleFederationPlugin like this (please see the above next.config.js file for complete confugration:

'swr/': {
  // requiredVersion: deps.swr,
  requiredVersion: '^2.2.2',
  eager: true,
},

Setting swr to deps.swr or '^2.2.2' does not fix the warning. The warning still recurs. I also tried setting the property name with swr and swr/ and this also does not work.

Any help would be appreciated on how this can be fixed.

See SWR 2.0 article here: https://swr.vercel.app/blog/swr-v2


Solution

  • After adding the below update, the warning goes away. In the next.config.js file, update the swr to:

    'swr/*': {
      singleton: true,
      requiredVersion: deps.swr,
      eager: true,
    },
    

    Adding the wildcard * after swr/* fixed the warning.