-
Notifications
You must be signed in to change notification settings - Fork 27.3k
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
Next server ESM support #25423
Comments
ProblemThe server doesn't handle 3rd party ES modules; instead it expects 3rd party dependencies to be compiled to CJS (commonjs). SolutionsYou can get around this by either... Option 1Using components/example.tsx import formatWithTimezone from "../utils/dates";
const date = formatWithTimezone(new Date(), "MMM d, yyyy hh:mma z");
export default function Example() {
return <div style={{ padding: 24 }}>{date}</div>;
} pages/index.tsx import dynamic from "next/dynamic";
const FormatDate = dynamic(() => import("../components/example"), { ssr: false });
export default function Home() {
return <FormatDate />;
} Option 2Or, better yet, transpiling the dependency to CJS for the server: next.config.js module.exports = {
webpack(config, { defaultLoaders, isServer }) {
if (isServer)
config.module.rules.unshift({
test: /\.js$/,
loader: defaultLoaders.babel,
include: /[\\/]node_modules[\\/](date-fns\/parseISO)[\\/]/,
});
return config;
},
}; Other thoughtsThis is not a bug with NextJS since it's server Webpack config is not/doesn't appear to be transpiling/handling 3rd party ESM dependencies (nor should it, as there may be compatibility issues), but instead a misstep?/miscalculation? with the library maintainers not factoring in that node's ES module support is still relatively new and is not universally adopted (on that note, all node versions prior to Node v13.2.0 wouldn't support ES modules -- at least not without the As of writing this, if I were to run this library on an API/web server running node, then the expected build output should/would be commonjs: example API or example web server. When you start using ES modules on the server, you'll quickly start running into compatibility issues with other libraries (for example, tsconfig-paths would have to support ESM to work with ts-node). |
Node v12 also supports ES modules. The last version to not support ES modules is unsupported (v10). Next.js should really support this, as ES modules are starting to be adopted widely. Some packages no longer support CJS. More and more people are asking for ES modules (just look at #9607).
A good title would be "Next server ESM support". People have said it before, and I am going to say it again: this has been part of the standards for years, and it has part of Node for a while. Why is NextJS not going to support it? Just because there "could be issues" doesn't mean that this shouldn't be implemented. Just because legacy/dead platforms like IE11 and node 10 don't support ESM, people who can/want to use modern platforms/things shouldn't be held back. See #22381 as well. |
Only versions v12.17.0+ and it's considerable experimental:
This means all versions prior to v12.17.0 wouldn't support ESM out of the box. While Node v10 is EOL, Node v12 is not: Node Versions.
On the same token, a company whose product relies upon legacy dependencies/legacy browser support shouldn't have to worry about potentially breaking/updating their entire codebase for an experimental feature that hasn't quite become standard across all current versions of node. It's very easy to say: "Just add support," without factoring in how that change may adversely affect other dependencies. For example, how does adopting ESM on a custom Node server affect test suites that use jest? As of now, Next still supports v10.13.0, but I have no doubt it'll fully support ESM in the near future. It just may take some time before full adoption occurs across the dependency tree/dev world. Until then, there appears to be at least some movement toward some ESM support: #22153 |
Node 12.0.0 has rudimentary support for ES modules, see changelog. v12.17.0 just unflags the support. v12.20.0 (or is it v12.22.0?) makes the support stable.
I see where you are coming from, and I understand that too. This could be hidden under a config option in Nonetheless, I think that there is no point arguing over who's right and who's not. Nobody's wrong. Everybody has something right to say. Trying to come up with a solution is more productive. |
Thanks @PythonCreator27 and @mattcarlotta for the information. This specific issue for me will be fixed in the next major version of date-fns which is nice, until then I've patched the library using https://www.npmjs.com/package/patch-package. This isn't the first module that caused a little bit of ESM module headache and I'm sure it won't be the last. I get why Next.js wouldn't support this use case, it isn't technically correct. My |
For those who are using yarn 2/3, here is an example with |
This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you. |
What version of Next.js are you using?
10.2.2
What version of Node.js are you using?
15.14.0
What browser are you using?
n/a
What operating system are you using?
macOS
How are you deploying your application?
next export
Describe the Bug
When exporting my application, I now get an ESM module error:
This error occurs in
date-fns
land, it doesn't seem like a Next.js specific bug, though this used to not happen in previous versions ofnext
and only began happening in version10.2.1+
(I've verified this happens in 10.2.2 and next@canary).When removing
"module": "esm/index.js",
fromdate-fns
anddate-fns-tz
this error still occurs which is pretty strange.I was able to get around this in my other production next app, by changing all
esm
files in date-fns to.mjs
, which again, doesn't seem relevant to next.js, but this did used to work, so no idea!Expected Behavior
My Next application should successfuly start/export
I understand this is likely a bug in date-fns or date-fns-tz, but this library used to work well in previous versions of Next and it no longer works
To Reproduce
OR
Over here:
https://github.com/hanford/next-date-fns-bug
....
I couldn't think of a super accurate title here. Hoping a contributor or someone with context could rename the title 😬
The text was updated successfully, but these errors were encountered: