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

Vite Module Federation中remoteEntry.js返回HTML页面导致模块加载失败的问题求助

Vite Module Federation中remoteEntry.js返回HTML页面导致模块加载失败的问题求助

大家好,我最近在尝试用Vite搭配vite-plugin-federation来搭建微前端架构,碰到了一个卡壳的问题,想过来请教下各位:

我目前有两个独立的应用:

  • Host App:运行在 http://localhost:5000
  • Remote App(会计模块):运行在 http://localhost:5002

我的配置情况

在Remote App的vite.config.ts里,我是这么配置federation的:

federation({
  name: 'accounting',
  filename: 'remoteEntry.js',
  exposes: {
    './AccountingPage': './src/pages/AccountingPage.tsx',
  },
  shared: ['react', 'react-dom'],
})

然后在Host App里,我用懒加载的方式导入这个远程页面:

const AccountingPage = lazy(() => import('accounting/AccountingPage'));

遇到的问题

当我启动Host App并尝试加载这个远程模块时,控制台直接抛出了错误:

Uncaught TypeError: Failed to fetch dynamically imported module: http://localhost:5002/assets/remoteEntry.js

更奇怪的是,我直接在浏览器地址栏访问http://localhost:5002/assets/remoteEntry.js时,返回的不是预期的JavaScript文件,而是Remote App的index.html页面!这显然直接搞崩了模块加载的流程。

想请教的问题

  • 我该怎么配置才能让Vite正确返回remoteEntry.js文件,而不是默认的HTML页面?
  • 是不是我在vite.config.ts或者vite-plugin-federation的设置里漏了什么关键配置,才能确保不管是开发环境还是生产环境,remoteEntry.js都能被正常访问到?

内容来源于stack exchange

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.07 13:34:39