Replies: 3 comments 3 replies
-
Referencing a comment on the PR but I figure this is a better place for discussion:
Why would This is just the config file, so it isn't itself transformed by webpack? I've found Webpack5 is necessary to support native ESM elsewhere in NextJS but that is for transpiler logic. Also I would argue that #15307 will be resolved by the changes in #22153 because the result of |
Beta Was this translation helpful? Give feedback.
-
Can I ask what the current status of this issue is? Are we still in figuring out the satisfying way to have default ESM support? Please help me understand what is the blocker here.
|
Beta Was this translation helpful? Give feedback.
-
Update: This has shipped! You can now use |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Update: This has shipped! You can now use
next.config.mjs
.Goals:
Background:
ECMAScript modules are the official standard format to package JavaScript code for reuse. As the ecosystem moves from commonjs towards ESM, the need for defining Next.js configuration as ESM will rise. Some libraries even started to publish ESM-only modules. Node.js acquired the capability to load ESM from commonjs and Next.js should leverage that to load configuration from config files in ESM format.
Implementation:
node.js supports loading ESM natively from version 12 on. If
next.config.mjs
exists in the project root, and the user is on node >=12, Next.js should use dynamicimport()
to load the default export ofnext.config.mjs
as the Next.js configuration, ornext.config.js
if the former does not exist. When on node <12 revert torequire
andnext.config.js
as is the current behaviour. The choice ofmjs
extension is in line with the node.js module resolution algorithm.Since
import()
is always asynchronous, this will require turning the config loading from a synchronous process into an asynchronous one. Innext
CLI this shouldn't be too difficult to convert. The challenge will be in custom server that can't boot up before config is loaded. This will probably require moving config loading into the.prepare
method.Out of scope:
Besides enabling ESM support, this will also serve as a basis to build further on:
"type": "module"
inpackage.json
to enable ESM for.js
filesnext.config.ts
when that node.js feature stabilizes.Open PR:
#22153
Beta Was this translation helpful? Give feedback.
All reactions