英文:
"Error: require() of ES Module" using Visx with Nextjs
问题
我使用 Visx 库在 Next.js 中构建图表。我使用 Visx 刻度,导入方式如下:
import { scaleBand, scaleLinear, scaleOrdinal } from "@visx/scale";
Visx 内部使用了 d3 的刻度,并使用 "require" 导入来访问它们,因此我收到了以下 Next.js 错误:
Error: require() of ES Module \node_modules\d3-scale\src\index.js from \node_modules\@visx\scale\lib\scales\band.js not supported. Instead change the require of index.js in \node_modules\@visx\scale\lib\scales\band.js to a dynamic import() which is available in all CommonJS modules.
我知道错误已经很明显了,但我想知道是否有除了更改库文件之外的其他解决方案,或者在任何情况下,哪个是最好的。
我还尝试更改 Visx 刻度的导入,但是我得到了另一个错误:
Cannot use import statement outside a module
英文:
I am using the Visx library to build charts in Nextjs. I am using the Visx scales, for which I import them as follows:
import { scaleBand, scaleLinear, scaleOrdinal } from "@visx/scale"
Internally, Visx uses d3's scales and is using "require" import to access them, so I get the following Nextjs error:
Error: require() of ES Module \node_modules\d3-scale\src\index.js from \node_modules\@visx\scale\lib\scales\band.js not supported. Instead change the require of index.js in \node_modules\@visx\scale\lib\scales\band.js to a dynamic import() which is available in all CommonJS modules.
I know the error is self-explanatory, but I would like to know if there is another solution besides changing the library's files or, in any case, what is the best one.
I also tried changing the imports of Visx scales, but I got another error:
Cannot use import statement outside a module
答案1
得分: 1
这是一个关于Visx 3.X.X
和Next的开放问题。
目前,将Visx包降级到它们的最新版本2.X.X
可以解决这个问题。
详细信息
根据目前的描述,这似乎是Visx包的一个开放问题。详细信息在此
总结一下:
- Visx 3 升级到了最新版本的d3
- 最新版本的d3 只支持ESM,而Visx使用CommonJS
- Next构建流程由于不匹配而引发了此错误
在该问题中提到了一些解决方法,涉及到Next 13的transpileModules
;然而,我只能通过将所有Visx包降级到它们的最新版本2.X.X
来解决这个问题。
英文:
TL;DR
This is an open issue with Visx 3.X.X
and Next.
For now, downgrading Visx packages to their latest 2.X.X
versions solves the problem.
Details
This appears to be an open issue with the Visx package at the time of this writing. Details here
In summary:
- Visx 3 updates to the latest version of d3
- The latest version of d3 is ESM-only, while Visx uses CommonJS
- The Next build pipeline throws this error due to the mismatch
There are some workarounds mentioned in the issue involving Next 13's transpileModules
; however, I was only able to solve the issue by downgrading all the Visx packages to their latest 2.X.X
versions.
答案2
得分: 0
确保 visx
包的版本匹配。我在我的 Next.js 项目中安装 @visx/stats
包时遇到了类似的 ESM 错误。当我尝试使用 @visx/stats
中的 scaleBand
函数来绘制箱线图时,出现了错误。经过一些调查,我发现错误的原因是 @visx/stats
和 @visx/visx
包之间的版本不匹配。
我通过降级 @visx/stats
的版本以与我使用的 @visx/visx
包的版本匹配来解决了这个问题。值得注意的是,如果您在项目中使用的其他包存在版本不匹配,也可能导致此错误。
如果您遇到类似的问题,我建议检查您的包之间是否存在版本不匹配,并确保它们彼此兼容。这可能需要调整一些包的版本以使它们相互匹配。
希望这个解释能帮助面对类似问题的人。
英文:
TLDR: ensure that the versions of visx
packages match.
I encountered a similar ESM error when I installed the @visx/stats
package in my Next.js project. The error occurred when I tried to use the scaleBand
function to draw boxplots from @visx/stats
. After some investigation, I discovered that the cause of the error was the version mismatch between @visx/stats
and @visx/visx
packages.
I resolved the issue by downgrading the version of @visx/stats
to match the version of @visx/visx
that I was using. It's worth noting that this error may also occur if there are version mismatches with other packages you are using in your project.
If you are experiencing a similar issue, I recommend checking for any version mismatches between your packages and ensuring that they are all compatible with each other. This may require adjusting the versions of some packages to match each other.
Hope this explanation helps anyone facing a similar problem.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论