You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用module-federation/nextjs-mf时NextJS宿主应用无法解析模块路径

Module Federation with nextjs-mf: Host Application Fails to Resolve Remote Module Paths

我用webpack的module-federation/nextjs-mf插件构建微前端架构,官方纯NextJS示例运行正常。当前搭建了1个宿主应用和多个远程应用,均基于NextJS、nextjs-mf插件及TypeScript。远程应用之间可以成功共享组件,但宿主应用出现无法解析模块路径的错误。

代码实现

动态导入远程组件的代码:

import dynamic from "next/dynamic"
const NextRemoteComponent = dynamic(
  () => import("remote_finances/next-remote-component"),
  { ssr: false }
)

宿主应用的next.config.js配置:

module.exports = {
  webpack5: true,
  webpack(config, options) {
    const { webpack, isServer } = options;
    config.experiments = { topLevelAwait: true };

    config.module.rules.push({
      test: /_app.js/,
      loader: '@module-federation/nextjs-mf/lib/federation-loader.js',
    });

    config.plugins.push(
      new webpack.container.ModuleFederationPlugin({
        remotes: {
          remote_finances: "remote_finances@http://localhost:8081/_next/static/chunks/remoteEntry.js",
          remote: 'remote@http://localhost:8081/_next/static/chunks/remoteEntry.js',
        },
        shared: {
          react: {
            singleton: true,
            eager: true,
            requiredVersion: false,
          },
        },
      }),
    );
    return config;
  },
  reactStrictMode: true,
  experimental: {
    outputStandalone: true
  },
  env: {
    BASE_URL : `https://qa-core-api.nordhen.com`,
  },
};

已尝试的解决方案

  • 修改多种.eslintrc及.eslintrc.json配置
  • 调整相关依赖包版本

目前不确定问题是否源于TypeScript、ESLint或Webpack配置,可提供更多代码或实现细节。

内容的提问来源于stack exchange,提问作者Isaí Madueño

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.23 08:15:37