如何让我的 "Fake ESM" Next 12 项目使用原生 ESM?

How Can I Make My "Fake ESM" Next 12 Project Use Native ESM?

我有一个完美运行的 Next.js 版本 11 站点,它使用 Babel/webpack 获得现代“ES 模块”语法:我整个应用程序中唯一的 require/CommonJS 语法是在 next.config.jsbabel.config.js.

我将它更新到 Next 12 并且效果很好,所以我想我会尝试摆脱那些剩余的需求,并通过将 "type": "module" 添加到我的 [=15] 来使我的项目完全 ESM =].

这给了我关于 next.config.js 如何必须使用 ESM 语法的错误,所以我将其更新为 ESM 语法......但现在我得到关于 Next 如何(显然?)使用 Common JS 的错误导入 babel.config.js?

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /home/me/project/client/babel.config.js require() of ES modules is not supported. require() of /home/me/project/client/babel.config.js from /home/me/project/client/node_modules/next/dist/build/babel/loader/get-config.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 babel.config.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /home/me/project/client/package.json.

因为我无法控制 Next,所以我不知道如何解决这个问题(Next 12 不应该默认使用 ESM 导入吗?)而且我在 babel.config.js 中的语法也没有似乎很重要:无论我使用 ESM 还是 Common JS,我都会得到同样的错误。

如有任何帮助,我们将不胜感激。

You're importing Next.js itself as an ES Module which is not supported yet.

参见:https://github.com/vercel/next.js/discussions/27876#discussioncomment-1163394