简体   繁体   English

将 D3.js 7.0.0 与 Next.js 11.0.1 一起使用时,如何解决“[ERR_REQUIRE_ESM]:必须使用导入加载 ES 模块”的问题?

[英]How do I resolve "[ERR_REQUIRE_ESM]: Must use import to load ES Module" when using D3.js 7.0.0 with Next.js 11.0.1?

When trying to use D3 with Next.js, I cannot get past this error when using D3.js v7.0.0 with Next.js v11.0.1.:尝试将 D3 与 Next.js 一起使用时,在将 D3.js v7.0.0 与 Next.js v11.0.1 一起使用时,我无法克服此错误:

[ERR_REQUIRE_ESM]: Must use import to load ES Module [ERR_REQUIRE_ESM]:必须使用import加载ES模块

  • I tried next-transpile-modules with no luck我尝试了next-transpile-modules没有运气
  • I got D3.js v7.0.0 to work with create-react-app but I need D3 working with Next.js v11.0.1我让 D3.js v7.0.0 与create-react-app一起工作,但我需要 D3 与 Next.js v11.0.1 一起工作

I installed D3.js using npm i d3 .我使用npm i d3安装了 D3.js。 I'm importing with import * as d3 from "d3" .我正在使用import * as d3 from "d3" Using Node v15.8.0 and React v17.0.2使用 Node v15.8.0 和 React v17.0.2

if you only need "import" from ES6+ features just add 'type': 'module' in your package.json file.如果您只需要从 ES6+ 特性中“导入”,只需在package.json文件中添加'type': 'module' Otherwise use babel for enabling all ES6+ features for your app.否则,请使用babel为您的应用启用所有 ES6+ 功能。

Since v12, Next.js has native support for ES modules.从 v12 开始,Next.js 原生支持 ES 模块。 So if anyone is facing this issue, just upgrade your Next.js version.因此,如果有人遇到此问题,只需升级您的 Next.js 版本。 D3 and other packages that provide only the ESM entrypoints are now fully supported without any need to transpile them.现在完全支持 D3 和其他仅提供 ESM 入口点的包,无需转换它们。

Reference: https://nextjs.org/blog/next-12#es-modules-support-and-url-imports参考: https : //nextjs.org/blog/next-12#es-modules-support-and-url-imports

This support was experimental in Next.js v11.1, and can be enabled using the following config:这种支持在 Next.js v11.1 中是实验性的,可以使用以下配置启用:

// next.config.js
module.exports = {
  // Prefer loading of ES Modules over CommonJS
  experimental: { esmExternals: true }
}

Reference: https://nextjs.org/blog/next-11-1#es-modules-support参考: https : //nextjs.org/blog/next-11-1#es-modules-support

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Next.js ERR_REQUIRE_ESM 中导入 ES 模块 - Import ES module in Next.js ERR_REQUIRE_ESM webpack: “[ERR_REQUIRE_ESM]: Must use import to load ES Module” - 但我用的是 import! - webpack: “[ERR_REQUIRE_ESM]: Must use import to load ES Module” - But I AM using import! 升级 Node JS 和 ERR_REQUIRE_ESM:必须使用 import 加载 ES Module: 'inheritsloose.js' of Babel Runtime - Upgrading Node JS and ERR_REQUIRE_ESM: Must use import to load ES Module: 'inheritsloose.js' of Babel Runtime 错误 [ERR_REQUIRE_ESM]: 必须使用 import 来加载 ES 模块 - Error [ERR_REQUIRE_ESM]: Must use import to load ES module SSR with Node, Webpack, React, Express, Material UI, React Router - 错误 [ERR_REQUIRE_ESM]: 必须使用导入加载 ES 模块 - SSR with Node, Webpack, React, Express, Material UI, React Router - Error [ERR_REQUIRE_ESM]: Must use import to load ES Module Next.js 上的 ERR_REQUIRE_ESM 和 Package.json 错误。 更新后。 怎么修? - ERR_REQUIRE_ESM and Package.json error on Next.js. after updating. How to fix? 错误 [ERR_REQUIRE_ESM]:ES 模块的 require() - Error [ERR_REQUIRE_ESM]: require() of ES Module 错误 [ERR_REQUIRE_ESM]: 使用 SSR (Inertiajs) 时需要 ES 模块 - Error [ERR_REQUIRE_ESM]: require() of ES Module while using SSR (Inertiajs) PM2 带 ES 模块。 错误:ERR_REQUIRE_ESM - PM2 with ES module. Error: ERR_REQUIRE_ESM 错误 [ERR_REQUIRE_ESM]:不支持来自 /app/commands/Image/meme.js 的 ES 模块 /app/node_modules/got/dist/source/index.js 的 require() - Error [ERR_REQUIRE_ESM]: require() of ES Module /app/node_modules/got/dist/source/index.js from /app/commands/Image/meme.js not supported
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM