zhangyuang/ssr

Pm2模式下, Require() of ES Module 报错

Closed this issue · 2 comments

详细描述你的问题

使用模板midway-react-ssr 新建项目仓库,加入antv后报错

指引如下:
https://ant-design-charts.antgroup.com/manual/getting-started

本地启动报错,后改成mode: csr模式,本地开发启动正常;

但是生产模式npm run prod:vite 启动报错,500:
ERROR 46435 [-/::ffff:127.0.0.1/-/106ms GET /] Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/johnson/work/midwayjs/my-ssr-project/node_modules/d3-array/src/index.js from /Users/johnson/work/midwayjs/my-ssr-project/node_modules/@antv/g2/lib/transform/utils/order.js not supported.
Instead change the require of index.js in /Users/johnson/work/midwayjs/my-ssr-project/node_modules/@antv/g2/lib/transform/utils/order.js to a dynamic import() which is available in all CommonJS modules.
at Hook._require.Module.require (/usr/local/lib/node_modules/pm2/node_modules/require-in-the-middle/index.js:101:39)
at Object. (/Users/johnson/work/midwayjs/my-ssr-project/node_modules/@antv/g2/lib/transform/utils/order.js:4:20)
at Hook._require.Module.require (/usr/local/lib/node_modules/pm2/node_modules/require-in-the-middle/index.js:101:39)
at Object. (/Users/johnson/work/midwayjs/my-ssr-project/node_modules/@antv/g2/lib/transform/stackY.js:6:17)
at Hook._require.Module.require (/usr/local/lib/node_modules/pm2/node_modules/require-in-the-middle/index.js:101:39)
at Object. (/Users/johnson/work/midwayjs/my-ssr-project/node_modules/@antv/g2/lib/transform/maybeStackY.js:4:18)
at Hook._require.Module.require (/usr/local/lib/node_modules/pm2/node_modules/require-in-the-middle/index.js:101:39)
at Object. (/Users/johnson/work/midwayjs/my-ssr-project/node_modules/@antv/g2/lib/transform/index.js:6:21)
at Hook._require.Module.require (/usr/local/lib/node_modules/pm2/node_modules/require-in-the-middle/index.js:101:39)
at Object. (/Users/johnson/work/midwayjs/my-ssr-project/node_modules/@antv/g2/lib/mark/interval.js:4:21)
at Hook._require.Module.require (/usr/local/lib/node_modules/pm2/node_modules/require-in-the-middle/index.js:101:39)
at Object. (/Users/johnson/work/midwayjs/my-ssr-project/node_modules/@antv/g2/lib/mark/index.js:4:18)
at Hook._require.Module.require (/usr/local/lib/node_modules/pm2/node_modules/require-in-the-middle/index.js:101:39)
at Object. (/Users/johnson/work/midwayjs/my-ssr-project/node_modules/@antv/g2/lib/lib/core.js:6:16)
at Hook._require.Module.require (/usr/local/lib/node_modules/pm2/node_modules/require-in-the-middle/index.js:101:39)
at Object. (/Users/johnson/work/midwayjs/my-ssr-project/node_modules/@antv/g2/lib/lib/index.js:4:14)
at Hook._require.Module.require (/usr/local/lib/node_modules/pm2/node_modules/require-in-the-middle/index.js:101:39)
at Object. (/Users/johnson/work/midwayjs/my-ssr-project/node_modules/@antv/g2/lib/index.js:18:15)
at Hook._require.Module.require (/usr/local/lib/node_modules/pm2/node_modules/require-in-the-middle/index.js:101:39)
at Object. (/Users/johnson/work/midwayjs/my-ssr-project/node_modules/@ant-design/plots/lib/index.js:30:27)
at Hook._require.Module.require (/usr/local/lib/node_modules/pm2/node_modules/require-in-the-middle/index.js:101:39)
at Object. (/Users/johnson/work/midwayjs/my-ssr-project/node_modules/@ant-design/charts/lib/index.js:17:14)
at Hook._require.Module.require (/usr/local/lib/node_modules/pm2/node_modules/require-in-the-middle/index.js:101:39)
at Module. (/Users/johnson/work/midwayjs/my-ssr-project/build/server/Page.server.js:13:14)
at Hook._require.Module.require (/usr/local/lib/node_modules/pm2/node_modules/require-in-the-middle/index.js:101:39)
at viteRender (/Users/johnson/work/midwayjs/my-ssr-project/node_modules/ssr-core/cjs/render.js:71:34)
at render (/Users/johnson/work/midwayjs/my-ssr-project/node_modules/ssr-core/cjs/render.js:27:38)
at Index.handler (/Users/johnson/work/midwayjs/my-ssr-project/dist/controller/index.js:21:52)
at /Users/johnson/work/midwayjs/my-ssr-project/node_modules/@midwayjs/core/dist/common/webGenerator.js:43:61
at async /Users/johnson/work/midwayjs/my-ssr-project/node_modules/koa-static-cache/index.js:53:36
at async /Users/johnson/work/midwayjs/my-ssr-project/node_modules/koa-static-cache/index.js:53:36
at async /Users/johnson/work/midwayjs/my-ssr-project/node_modules/koa-static-cache/index.js:53:36
at async bodyParser (/Users/johnson/work/midwayjs/my-ssr-project/node_modules/koa-bodyparser/index.js:78:5)
at async session (/Users/johnson/work/midwayjs/my-ssr-project/node_modules/@midwayjs/session/dist/middleware/session.js:137:17)
at async /Users/johnson/work/midwayjs/my-ssr-project/node_modules/@midwayjs/core/dist/baseFramework.js:273:40
at async /Users/johnson/work/midwayjs/my-ssr-project/node_modules/@midwayjs/core/dist/baseFramework.js:267:24
at async midwayRootMiddleware (/Users/johnson/work/midwayjs/my-ssr-project/node_modules/@midwayjs/koa/dist/framework.js:100:13) {
code: 'ERR_REQUIRE_ESM'
}

环境:Mac m1 + node 18+

期望的结果

希望提供一个解决方案

当前使用的版本

最新脚手架模板:

复现仓库地址

https://github.com/zhao-core/my-ssr-project-temp
npm run prod:vite 访问报错

你本人对问题可能的原因判断(如果你能大概判断的话)

require() of ES Module

import type { UserConfig } from 'ssr-types'

const userConfig: UserConfig = {
  whiteList: [/@ant-design\/charts/]
}

export { userConfig }

这个是解决require() of ES Module, but 这个组件不支持ssr,如何降级到csr自行阅读文档相关介绍