简体   繁体   English

在 Next.js ERR_REQUIRE_ESM 中导入 ES 模块

[英]Import ES module in Next.js ERR_REQUIRE_ESM

I came upon this error when trying to use ky in a Next.js project:我在 Next.js 项目中尝试使用ky时遇到了这个错误:

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /foo/node_modules/ky/index.js错误 [ERR_REQUIRE_ESM]: 必须使用 import 来加载 ES 模块:/foo/node_modules/ky/index.js

I think the problem is that Webpack (or Babel) is transforming all import s to require() s but ky is a pure ES module .我认为问题在于 Webpack (或 Babel)正在将所有import转换为require() ,但ky是纯 ES 模块

I got it working by dynamically importing ky before using it but it's not elegant nor efficient.我通过在使用它之前动态导入ky来让它工作,但它既不优雅也不高效。

const handleFormSubmit = async (event) => {
  const ky = (await import("ky")).default;

  const response = await ky
    .get('http://localhost/api/foo')
    .json();
};

Any suggestions?有什么建议么?

Since ky is exported as ESM you can transpile it with next-transpile-modules in next.config.js .由于ky被导出为 ESM,您可以在 next.config.js 中使用next-transpile-modules对其进行next.config.js

// next.config.js
const withTM = require('next-transpile-modules')(['ky']);

module.exports = withTM(/* your Next.js config */);

You can try upgrading nextjs to v11.1.0, it has some support for ESM.您可以尝试将 nextjs 升级到 v11.1.0,它对 ESM 有一些支持。

See: https://github.com/vercel/next.js/pull/27069见: https://github.com/vercel/next.js/pull/27069

More info in this issue discussion from https://github.com/vercel/next.js/issues/9607#issuecomment-906155992此问题讨论中的更多信息来自https://github.com/vercel/next.js/issues/9607#issuecomment-906155992

Since Next.js 12, support for ESM modules is automatically enabled.自 Next.js 12 起,自动启用对 ESM 模块的支持。 See #29878 .请参阅#29878

暂无
暂无

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

相关问题 将 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? 升级 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]:ES 模块的 require() - Error [ERR_REQUIRE_ESM]: require() of ES Module 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! 错误 [ERR_REQUIRE_ESM]: 必须使用 import 来加载 ES 模块 - Error [ERR_REQUIRE_ESM]: Must use import to load ES module PM2 带 ES 模块。 错误:ERR_REQUIRE_ESM - PM2 with ES module. Error: ERR_REQUIRE_ESM 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 错误 [ERR_REQUIRE_ESM]: 使用 SSR (Inertiajs) 时需要 ES 模块 - Error [ERR_REQUIRE_ESM]: require() of ES Module while using SSR (Inertiajs) 错误 [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 Next.js 上的 ERR_REQUIRE_ESM 和 Package.json 错误。 更新后。 怎么修? - ERR_REQUIRE_ESM and Package.json error on Next.js. after updating. How to fix?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM