简体   繁体   English

错误:使用带有 next.js(和打字稿)的重新图表时,不支持 ES 模块的 require()

[英]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.

相关问题 使用 typescript 时“不支持 ES 模块 [...] 的 require()” - "require() of ES Module [...] is not supported" when using typescript [错误]:不支持 ES 模块的 require() - [ERROR]: require() of ES Module not supported Next.js - 导入 TypeScript 模块时找不到模块 - Next.js - module not found when importing TypeScript module 使用带有 Next.js 的 Typescript 找不到具有相应类型声明的模块 - Module not found with corresponding type declarations using Typescript with Next.js 在 Nest.js 中对 ESM 模块使用导入会给出 [ERR_REQUIRE_ESM]:不支持 ES 模块的 require() - Using import for an ESM Module in nest.js gives [ERR_REQUIRE_ESM]: require() of ES Module not supported 将 Webpack5 与 Next.JS 应用程序一起使用时出现 Storybook 错误 + Typescript - Storybook error when using Webpack5 with Next.JS app + Typescript Next.js + Typescript + mongoose 使用`let cached = global.mongoose` 时出错 - Next.js + Typescript + mongoose error when using `let cached = global.mongoose` 错误 [ERR_REQUIRE_ESM]: ES 模块的 require()... 不支持 - Error [ERR_REQUIRE_ESM]: require() of ES Module ... not supported 在 Next.js 上使用 jest 找不到模块错误 - Cannot find module error using jest on Next.js 为什么我的 typescript 节点模块在安装到我的 Next.js 应用程序时会触发“可能需要适当的加载程序”错误? - Why does my typescript node module when installed into my Next.js app trigger a “may need an appropriate loader” error?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM