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

Adonis Js+Inertia Js中SSR运行失败求助:ES模块兼容错误

在AdonisJS + Vue3 + Inertia项目中启用SSR遇到ES模块兼容错误

我在基于仓库https://github.com/eidellev/inertiajs-adonisjs的AdonisJS和Vue3项目中尝试启用SSR时遇到错误,关闭SSR后应用可正常运行。

错误信息

require() of ES Module C:\Users\project\software\node_modules\@inertiajs\vue3\dist\index.js from C:\Users\project\software\inertia\ssr\ssr.js not supported. index.js is treated as an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which declares all .js files in that package scope as ES modules. Instead rename index.js to end in .cjs, change the requiring code to use dynamic import() which is available in all CommonJS modules, or change "type": "module" to "type": "commonjs" in C:\Users\project\software\node_modules\@inertiajs\vue3\package.json to treat all .js files as CommonJS (using .mjs for all ES modules instead).

inertia/ssr/ssr.js
/*!**********************************!*\
  !*** external "@inertiajs/vue3" ***!
  \**********************************/
/***/ ((module) => {

module.exports = require("@inertiajs/vue3");

/***/ }),

/***/ "@vue/server-renderer":
/*!***************************************!*\

解决方法

1. 改用ES模块语法编写SSR入口文件

  • 将inertia/ssr/ssr.js重命名为inertia/ssr/ssr.mjs
  • 把文件内的CommonJS语法替换为ES模块导入:
import { createInertiaApp } from '@inertiajs/vue3';
import { createSSRApp } from 'vue';
import { renderToString } from '@vue/server-renderer';

// 其余代码同步改为ES模块格式
  • 更新AdonisJS中SSR相关配置,指向新的.mjs入口文件

2. 调整Vite构建配置输出CommonJS格式

如果项目用Vite打包SSR资源,修改vite.config.js中的SSR配置:

export default {
  // 其他配置...
  ssr: {
    format: 'cjs',
  },
};

重新构建后,SSR bundle会以CommonJS格式输出,规避模块兼容问题

3. 临时修改依赖包配置(不推荐)

找到node_modules/@inertiajs/vue3/package.json,将"type": "module"改为"type": "commonjs"。注意此方法仅为临时方案,后续执行npm install或yarn install时配置会被覆盖,仅用于快速验证问题。

内容的提问来源于stack exchange,提问作者marcelo.delta

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.22 12:05:22