-
Notifications
You must be signed in to change notification settings - Fork 27k
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support ES module format (ESM) in next.config.js #9607
Comments
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. |
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 |
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:
|
same here tried to migrate project to use import just to fail on next.config.js |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Any progress in implementation of esm syntax so far? |
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 |
Nice! 👍 If you want to retain |
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 |
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 |
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. |
Im running into this issue too. no matter how I turn it, the combination just doesn't work. |
For information, ky will move to esm only... next-transpile-modules can be used meanwhile, but esm would be really appreciated. |
ESM is cool, but what about typescript? Will that be |
Since Next.js v11.1.0, you can use the {
experimental: {
esmExternals: "loose",
},
} |
@Richienb did you find how to enable directory imports? Index imports aren't supported in ESM so far and it's unclear whether some magic Node flag can override that. It's totally inconvenient to develop without |
Last time I checked, directory imports are not actually an ESM feature and thus would not work. You are able to import by file paths however. |
@Richienb there's a flag
|
same here: nextjs: 11.1.0 I want use a remark plugins , some code like this :
|
I'm having the same issue with TypeScript I tried adding Using Dynamic import fixes it though: const ReactMarkdown = dynamic(() => import('react-markdown')); |
@nedkelly are you doing this inside next config? |
Same problem here, unable to get remark-slug or rehype-slug working due to this error. |
Just use const a = import('react-markdown') where ever you want to use the dependency |
You can't use import inside next config.. |
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. |
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? |
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. |
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. |
That's totally different from saying "let's close the comments". Anyway, this is the issue i want to follow. |
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. |
@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?
|
@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). |
I've opened a PR that changes from |
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
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
The text was updated successfully, but these errors were encountered: