首页 > 解决方案 > Webpack Module Federation 共享单例模块 @angular/common 的 11.xx 版本不满意(需要 ^7.2.0)

问题描述

我正试图让我相当复杂的单体应用程序与模块联盟一起工作。我的 webpack 配置看起来像这样

   plugins: [
    new ModuleFederationPlugin({
        remotes: {
            "mfe1": "mfe1@http://localhost:3000/remoteEntry.js",
        },
        shared: {
          "@angular/core": { singleton: true, strictVersion: true },
          "@angular/common": { singleton: true, strictVersion: true },
          "@angular/router": { singleton: true, strictVersion: true },
          ...sharedMappings.getDescriptors()
        }
    }),
    sharedMappings.getPlugin(),
  ],

在微前端方面共享是相同的。当我尝试运行应用程序时,我得到:

错误:共享单例模块 @angular/common 的 11.2.1 版本不满意(需要 ^7.2.0)

在此之前,我收到了类似的错误消息,但针对 angular/core。我通过重新运行纱线并根据不同的角度/核心版本修复库产生的所有警告来解决这个问题。

但是由于 fpr angular/common 错误,我被卡住了。我不知道如何找出哪个库可能会产生该错误。

标签: angularwebpackwebpack-5webpack-module-federation

解决方案


您可能应该requiredVersion 在每个共享项目上指定。如果你不指定它,webpack 将尝试不仅从你的主 package.json 中确定版本,而且从在你的 node_modules 中使用这些角度库的任何 npm 包中确定版本。

将会发生的情况是,每当您导入使用 Angular 的第三方模块时,它都会扫描 package.json 以查找该模块并基于此添加另一个条目版本范围映射。这可能会导致不需要的行为,并且可能是您所看到的原因。


推荐阅读