[英]Error: require() of ES module is no supported when using recharts with next.js (and typescript)
I see some questions of this kind, but none of them really gets my problem.我看到了一些此类问题,但没有一个真正解决我的问题。 I'm developing a webapp using next.js (working with typescript).
我正在使用next.js (使用打字稿)开发一个网络应用程序。 In my app uses recharts , but the compilation fails with this error:
在我的应用程序中使用recharts ,但编译失败并出现此错误:
Error: Must use import to load ES Module: project_path\node_modules\d3-shape\src\index.js
require() of ES modules is not supported.
require() of project_path\node_modules\d3-shape\src\index.js from project_path\node_modules\recharts\lib\shape\Symbols.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from project_path\node_modules\d3-shape\package.json.
Now, I'm using next.js 12 which supports ES modules out of the box, no additional config needed.现在,我使用的是 next.js 12,它开箱即用地支持 ES 模块,不需要额外的配置。 As I understand it, the problem is that
d3-shape
is now imported as ESM, but recharts
, which uses it still require
s it instead of importing it (this is true, the "complied" recharts package does use require()
)据我了解,问题是
d3-shape
现在作为 ESM 导入,但是使用它的recharts
仍然require
它而不是导入它(这是真的,“complied” recharts package 确实使用require()
)
So the problem is not my App, but the way recharts is importing d3-shapes, but how can I solve it?所以问题不是我的应用程序,而是 recharts 导入 d3-shapes 的方式,但我该如何解决呢? It doesn't make sense that I'm the only one suffering from it.
我是唯一一个受苦的人,这是没有意义的。
I guess I can fork recharts
and make sure it imports d3-shapes as esm modules (adding type: "module"
to the package.json file) but this is very ugly.我想我可以 fork
recharts
并确保它将 d3-shapes 导入为 esm 模块(将type: "module"
添加到 package.json 文件),但这非常难看。
Any one has any ideas?有人有什么想法吗? I really don't want to go and use other charting packages...
我真的不想 go 并使用其他图表包...
https://github.com/recharts/recharts/issues/2918 https://github.com/recharts/recharts/issues/2918
talking about the same issue on the corresponding link.在相应的链接上谈论同样的问题。
Downgrading the package version of recharts to "2.1.12" will solve the problem.将 package 版本的 recharts 降级到“2.1.12”将解决问题。 In my case, it's solved.
就我而言,它已经解决了。
Dynamic Imports seems to fix this issue.动态导入似乎解决了这个问题。
import dynamic from "next/dynamic";
const Chart = dynamic(() => import("features/dashboard/Chart"), { ssr: false });
If you've reached here with the same error but with Remix Run, see this section of the docs: https://remix.run/docs/en/v1/pages/gotchas#importing-esm-packages如果您到达此处时出现相同的错误但使用 Remix Run,请参阅文档的这一部分: https://remix.run/docs/en/v1/pages/gotchas#importing-esm-packages
This is how my config file looked to make it compile:这是我的配置文件看起来如何编译:
//in remix.config.js
/** @type {import('@remix-run/dev').AppConfig} */
module.exports = {
ignoredRouteFiles: ["**/.*"],
serverDependenciesToBundle: [
"recharts",
"d3-shape",
"d3-scale",
"d3-path",
"d3-array",
"d3-time",
"d3-format",
"d3-interpolate",
"d3-time-format",
"d3-color",
"internmap",
],
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.