Support ES module format (ESM) in next.config.js #32239
Replies: 64 comments 29 replies
-
Node 13 is not yet suitable for production environments, and using the mjs extension is intrusive. Recently I needed a custom server to use better express middleware features, but I have no way to use ESM in any custom server related files. I hope to add an example about using ESM and nodemon when customizing the server. |
Beta Was this translation helpful? Give feedback.
-
While I’d love to see ESM support globally in Next, this issue is just about the Let’s not get into a discussion about the merits of |
Beta Was this translation helpful? Give feedback.
-
I have the same issue. Do you have any idea how to fix it? I previously used node 8.10.0 and now changed to 12.16.1 and when I yarn dev it keeps returning me the error in one of my npm packages.
I added type:module to package.json but it says:
|
Beta Was this translation helpful? Give feedback.
-
same here tried to migrate project to use import just to fail on next.config.js |
Beta Was this translation helpful? Give feedback.
This comment was marked as spam.
This comment was marked as spam.
This comment was marked as spam.
This comment was marked as spam.
-
Any progress in implementation of esm syntax so far? |
Beta Was this translation helpful? Give feedback.
-
For anyone running up against this issue. I am using a custom server for complete flexibility in my build. Node files are located in a top level dir called
As noted in the structure above, place an empty |
Beta Was this translation helpful? Give feedback.
-
Nice! 👍 If you want to retain |
Beta Was this translation helpful? Give feedback.
-
Since Node.js 12.17.0 ES modules have been enabled by default (a flag is no longer required). So ES modules are now available in an LTS version of Node.js: Also it looks like you can use normal |
Beta Was this translation helpful? Give feedback.
-
Is the use of next-transpile-modules still the only way to include modules that only provide ESM? I tried switching my project to In case it is relevant, my project uses TypeScript. I had to include My final goal is to use |
Beta Was this translation helpful? Give feedback.
-
I ran into a similar issue. If any of the folders inside my Next.js project use {type:"module"}, then The silly thing is that Webpack/React uses ES Module format (import instead of require). So this is very strange. Next.js server really should use ESM by default, because it's working with an ESM filesystem. |
Beta Was this translation helpful? Give feedback.
-
Im running into this issue too. no matter how I turn it, the combination just doesn't work. |
Beta Was this translation helpful? Give feedback.
-
For information, ky will move to esm only... next-transpile-modules can be used meanwhile, but esm would be really appreciated. |
Beta Was this translation helpful? Give feedback.
-
Created an RFC (#22381) and started an implementation (#22153) for this |
Beta Was this translation helpful? Give feedback.
-
You can't use import inside next config.. |
Beta Was this translation helpful? Give feedback.
-
No, and to be honest, using dynamic import is a little unstable, it sometimes fails to load so it's not really a fix. And it doesn't work in next.config.js. |
Beta Was this translation helpful? Give feedback.
-
No disrespect to the participants, but I think too many people misunderstand this issue, resulting in all sorts of irrelevant ESM problems being reported here. Can commenting be limited to collaborators, please? |
Beta Was this translation helpful? Give feedback.
-
So we are all stupid. Ok. Anyway the issue is pretty clear, any nextjs plugin which are now modules are totally unusable because nextjs config file doesn't support modules. |
Beta Was this translation helpful? Give feedback.
-
This issue was for allowing the The best thing to do is to move this part of the conversation to a seperate issue and guide people who want to talk about that specific thing there. To people who are having trouble loading modules written in ESM: create a reproduction repository and create a new bug report. As for general discussion about it, we now have #29348. |
Beta Was this translation helpful? Give feedback.
-
That's totally different from saying "let's close the comments". Anyway, this is the issue i want to follow. |
Beta Was this translation helpful? Give feedback.
-
Because Next configs are modules which must be loaded, it will take adding full ESM support to Next to support this for the reasons mentioned in this comment:
I have an experimental branch going to explore this, but Jest ESM support is shoddy and ultimately blocking progress. |
Beta Was this translation helpful? Give feedback.
-
@ctjlewis Thank you for sharing your exploration. Based on it, do you think anything else will be straightforward in rewriting CJS to ESM when Jest's ESM support is solved?
|
Beta Was this translation helpful? Give feedback.
-
@rnarkk There are a few obstacles (some related to TS itself, which we may be able to workaround with Node12 resolution as of next release) to getting Next to that point, which I will detail in a separate issue and link here, but the way I'd propose implementing it, you would just need to set For TS projects, you'd want to configure your local project settings to use the new Node12 resolution, so that it forces you to write complete ESM-compatible import specifiers (or use something like tszip to rewrite them for you, which is what I do). |
Beta Was this translation helpful? Give feedback.
-
I've opened a PR that changes from |
Beta Was this translation helpful? Give feedback.
-
@ctjlewis Can you please give us an update on any progress regarding ESM support for |
Beta Was this translation helpful? Give feedback.
-
What happened on #29935? Can the next server be upgraded to generate ESM imports and such? Is there a PR somewhere to enable ESM output for NextJS? |
Beta Was this translation helpful? Give feedback.
-
I applied this solution to Master CSS's AOT compiler and it worked fine. import crossImport from 'cross-import'
const configModule = crossImport('next.config.{js,cjs,mjs,ts}')
const config = configModule.default || configModule Check out the feature request #44324 |
Beta Was this translation helpful? Give feedback.
-
OMG, this issue since 2019 year... |
Beta Was this translation helpful? Give feedback.
-
For anyone coming to this (outdated) discussion, there are solutions for ESM in Next.js config already now:
|
Beta Was this translation helpful? Give feedback.
-
Feature request
With Node 12.17.x landing native support for ES modules, I would expect
next.config.js
to support ES module format in addition to CJS.When using
type: module
inpackage.json
on Node v13.1.0 and an ESMnext.config.js
format, Next throws an error due to the config resolution logic relying onrequire
.Describe the solution you'd like
Next should follow Node’s module semantics and allow either CJS or ESM file, depending on user configuration.
Currently,
next.config.js
uses CJS withrequire
andmodule.exports
. ESM format would allow users toimport
dependencies andexport default
the config object.This theoretically also opens the door to mirror Node’s explicit file extension pattern, which would suggest support for
next.config.cjs
andnext.config.mjs
files. This is likely controversial, but it is part of the language.Describe alternatives you've considered
As statically analyzing the native module format of
next.config.js
likely introduces a fair amount of complexity, this could be used as an opportunity to introduce "superset" config files (ESM, TypeScript [see #5318, #8044]) which are transpiled toCJS
, emitted to thedistDir
(.next
), and read from that location. This would also allow users pinned versions of Node below 13 to benefit from ESM format or strongly typed config files (see @stencil/core for prior art.)Additional context
The following error is thrown, minimum reproducible files below.
package.json
next.config.js
Beta Was this translation helpful? Give feedback.
All reactions