-
Notifications
You must be signed in to change notification settings - Fork 27.8k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
ESM in .mjs files cause a dev mode runtime error #17806
Comments
In the meantime, is there any workaround to use .mjs files in the dev mode? I'm trying to introduce Next.js to an existing project, but this prevents me from doing so. Please advise. |
Am I the single one here who doesn’t understand why choose |
@jerrygreen Next.js must support standard Node.js file extensions such as Going further, Next.js should only support ESM in
Just because Next.js has the ability to hack together webpack and Babel configs to make non standard things work due to a build step, doesn't mean it should. It's better for the ecosystem if all our tools work to common standards; e.g. you want to be able to use in a Next.js project the same ESLint config you use for linting imports in your published Node.js packages. |
Oh, now I can finally see the reasoning! Yeah, I actually agree it should be supported, and less webpack hacks used: it's better to be handled by node since it does support all that too. Well, good thing Timer added this to the backlog milestone so hopefully it will be fixed sometime soon |
I have recently run into this issue as well, using In my use case, NextJS is just a part of a larger monorepo, all written with ESM. I definitely support the comment
In most cases it is great because it allows features like That twitter link #17806 (comment) actually appears to have fixed it for me, but it would be great to see this as a first-class feature and "just work" in NextJS without extra setup. I will say I disagree with
While that is most "correct", that would be breaking for most usage, which is writing ESM and expect it to automagically transpile into CommonJS. If someone is deploying NextJS on its own (e.g. via Vercel, no custom server, no monorepo...) then Babels unambiguous sourceType is plenty acceptable to me |
This comes from @joostdecock 's fork commit 4d854f6e9817f9fa9c83fa0f0566dd8cb0aba876 Merge: 66ef672 8ca954d Author: joostdecock <joost@joost.at> Date: Sat Jul 31 15:18:40 2021 +0200 Merge branch 'develop' of https://github.com/joostdecock/freesewing into joostdecock-develop commit 8ca954d Author: joostdecock <joost@joost.at> Date: Sat Jul 31 15:05:07 2021 +0200 chore: Final lingering changes prior to import commit 045bd9b Author: joostdecock <joost@joost.at> Date: Sat Jul 31 15:04:48 2021 +0200 chore: Flat import of markdown repo files commit 897bb75 Author: joostdecock <joost@joost.at> Date: Sat Jul 31 15:04:15 2021 +0200 chore: Removed markdown submodule commit 52c7158 Author: joostdecock <joost@joost.at> Date: Sat Jul 31 15:03:17 2021 +0200 chore: Removed markdown submodule commit 1e722d2 Author: joostdecock <joost@joost.at> Date: Wed Jul 28 18:07:13 2021 +0200 fix: Handle missing language commit 61ebf0c Author: joostdecock <joost@joost.at> Date: Wed Jul 28 18:07:01 2021 +0200 fix: Handle missing t method commit a264ab3 Author: joostdecock <joost@joost.at> Date: Wed Jul 28 18:03:26 2021 +0200 fix: pass language to mdx loadFile commit e33014a Author: joostdecock <joost@joost.at> Date: Wed Jul 28 18:03:03 2021 +0200 fix: Allow collapsing active page commit 2f4fc93 Author: joostdecock <joost@joost.at> Date: Wed Jul 28 17:39:23 2021 +0200 fix: margin-auto for images commit 0aa139f Author: joostdecock <joost@joost.at> Date: Wed Jul 28 17:11:05 2021 +0200 fix: Handle missing author commit 88eb14a Author: joostdecock <joost@joost.at> Date: Wed Jul 28 17:02:31 2021 +0200 feat(i18n): Added showcase commit 64e951f Author: joostdecock <joost@joost.at> Date: Wed Jul 28 17:02:08 2021 +0200 feat: Added showcase posts to tree commit 864ea03 Author: joostdecock <joost@joost.at> Date: Wed Jul 28 16:16:58 2021 +0200 chore: Updated config commit 1e0136f Author: joostdecock <joost@joost.at> Date: Tue Jul 27 17:57:50 2021 +0200 fix: Better mobile search commit e2348b7 Author: joostdecock <joost@joost.at> Date: Tue Jul 27 17:47:29 2021 +0200 fix: Better mobile menu sizing commit 4dafc17 Author: joostdecock <joost@joost.at> Date: Mon Jul 26 19:04:01 2021 +0200 wip: Fixes for deploy commit 2e9d4fc Author: joostdecock <joost@joost.at> Date: Sun Jul 25 18:39:32 2021 +0200 chore: Ported latest changes to dev commit a90d417 Author: joostdecock <joost@joost.at> Date: Sun Jul 25 18:21:27 2021 +0200 chore: Reworking i18n strings commit f3e8dfb Author: joostdecock <joost@joost.at> Date: Sun Jul 25 18:21:15 2021 +0200 feat: showcase posts commit c803cc2 Author: joostdecock <joost@joost.at> Date: Sat Jul 24 16:51:45 2021 +0200 chore: Work on navbar commit 36a35a7 Author: joostdecock <joost@joost.at> Date: Sat Jul 24 14:56:56 2021 +0200 chore: Better mdx code sharing commit 6b7b3d2 Author: joostdecock <joost@joost.at> Date: Sat Jul 24 14:23:29 2021 +0200 chore: Better strapi code sharing commit 8208d93 Author: joostdecock <joost@joost.at> Date: Sat Jul 24 13:53:01 2021 +0200 chore: Updated to strapi changes commit 7050629 Author: joostdecock <joost@joost.at> Date: Wed Jul 21 19:13:05 2021 +0200 wip: MDX support across sites commit b5dc4b5 Author: joostdecock <joost@joost.at> Date: Tue Jul 20 19:46:37 2021 +0200 feat: Allow collapsing of aside menu commit c0869b0 Author: joostdecock <joost@joost.at> Date: Sun Jul 18 20:24:36 2021 +0200 wip: Better navigation commit 8562da2 Author: joostdecock <joost@joost.at> Date: Sun Jul 18 20:05:32 2021 +0200 wip: Re-working tree navigation commit 260cadd Author: joostdecock <joost@joost.at> Date: Sun Jul 18 18:06:28 2021 +0200 wip: Re-working tree navigation commit 1651adf Author: joostdecock <joost@joost.at> Date: Sun Jul 18 16:03:04 2021 +0200 wip: Work on tree navigation commit c9121c6 Author: joostdecock <joost@joost.at> Date: Sun Jul 18 10:56:08 2021 +0200 wip: Working on next sites commit 37b34e1 Author: joostdecock <joost@joost.at> Date: Sun Jul 18 10:43:23 2021 +0200 wip: Still working on getting next to play nice commit dd23727 Author: joostdecock <joost@joost.at> Date: Sat Jul 17 19:53:52 2021 +0200 wip: Various build changes Disabled the tailwind JIT mode as it was giving problems Moved to next-standard way of using import aliases Added a prebuild step to load mdx/strapi/tree content This is a WIP commit d02630f Author: joostdecock <joost@joost.at> Date: Sat Jul 17 15:36:40 2021 +0200 breaking(i18n): Switched to named exports, renamed files I guess this makes it official: Merging in this fork will require a major version change because there are a bunch of breaking changes here. - We now use named exports in i18n - We export languages individually to facilitate tree shaking - We pre-build all strings, so we don't need to ship with dependencies commit a00eeaa Author: joostdecock <joost@joost.at> Date: Fri Jul 16 17:30:03 2021 +0200 feat: Support for MDX in blog posts commit afaa1fc Author: joostdecock <joost@joost.at> Date: Fri Jul 16 12:22:20 2021 +0200 feat: Initial setup of freesewing.org commit b9b7f43 Author: joostdecock <joost@joost.at> Date: Thu Jul 15 19:35:58 2021 +0200 chore: Prevent pruning of theme colors commit c642ba2 Author: joostdecock <joost@joost.at> Date: Thu Jul 15 18:59:42 2021 +0200 chore: Style tweaks commit d5defa5 Author: joostdecock <joost@joost.at> Date: Thu Jul 15 17:27:08 2021 +0200 chore: Tweaks to component styles commit 654c4d3 Author: joostdecock <joost@joost.at> Date: Tue Jul 13 18:38:12 2021 +0200 chore: Tweaks to mobile view commit 49d3516 Author: joostdecock <joost@joost.at> Date: Mon Jul 12 16:55:13 2021 +0200 chore: Updated dependencies commit 9bcbdcb Author: joostdecock <joost@joost.at> Date: Mon Jul 12 16:45:29 2021 +0200 fix: Submodule settings commit 9072805 Author: joostdecock <joost@joost.at> Date: Mon Jul 12 16:44:04 2021 +0200 chore: Initial build commit 4f248ed Author: joostdecock <joost@joost.at> Date: Mon Jul 12 15:58:13 2021 +0200 fix(pattern-info): Order of patterns commit 65a7d6b Author: joostdecock <joost@joost.at> Date: Mon Jul 12 15:26:59 2021 +0200 chore: Switched submodule to ssh commit f776fae Author: joostdecock <joost@joost.at> Date: Mon Jul 12 15:26:47 2021 +0200 wip: 2022 commit 052dbc6 Author: Joost De Cock <joost@joost.at> Date: Sat Jul 10 18:03:22 2021 +0200 chore: Theme tweaks commit 2e43b0d Author: Joost De Cock <joost@joost.at> Date: Sat Jul 10 17:46:44 2021 +0200 feat(nextjs): Added footer commit 08a99e4 Author: Joost De Cock <joost@joost.at> Date: Sat Jul 10 15:56:16 2021 +0200 feat(nextjs): Added pattern styling commit b3dc673 Author: Joost De Cock <joost@joost.at> Date: Sat Jul 10 15:04:13 2021 +0200 chore: Search improvements commit 74f3667 Author: Joost De Cock <joost@joost.at> Date: Sat Jul 10 15:03:22 2021 +0200 Fix: Workaround for NextJS ESM bug See vercel/next.js#17806 commit 06fc77f Author: Joost De Cock <joost@joost.at> Date: Fri Jul 9 17:48:25 2021 +0200 feat(nextjs): Added PrevNext component commit 6f694da Author: Joost De Cock <joost@joost.at> Date: Fri Jul 9 15:49:47 2021 +0200 feat(nextjs): Let tree component handle ReadMore commit 2347c19 Author: Joost De Cock <joost@joost.at> Date: Fri Jul 9 09:32:49 2021 +0200 feat(nextjs): Close search button on mobile commit ca72776 Author: Joost De Cock <joost@joost.at> Date: Fri Jul 9 08:50:21 2021 +0200 feat(nextjs): Implemented keyboard-driven search commit 5bf952d Author: Joost De Cock <joost@joost.at> Date: Tue Jul 6 17:10:39 2021 +0200 chore: Removed lingering console.log commit 70397a3 Author: Joost De Cock <joost@joost.at> Date: Tue Jul 6 15:50:19 2021 +0200 wip(nextjs): Save prior to moving to a different machine commit 5775d8e Author: Joost De Cock <joost@joost.at> Date: Tue Jul 6 09:23:30 2021 +0200 wip(nextjs): Some further style tweaks commit a30ff5c Author: Joost De Cock <joost@joost.at> Date: Mon Jul 5 14:48:28 2021 +0200 feat(nextjs): Allow toggling top-level navigation commit 469d396 Author: Joost De Cock <joost@joost.at> Date: Mon Jul 5 14:47:45 2021 +0200 fix(nextjs): Handle unknown languages in prism commit e1f8d08 Author: Joost De Cock <joost@joost.at> Date: Mon Jul 5 14:47:15 2021 +0200 feat(nextjs): Added down icon commit c610c56 Author: Joost De Cock <joost@joost.at> Date: Mon Jul 5 10:00:17 2021 +0200 feat(nextjs): Added syntax highlighting commit 9d5cb83 Author: Joost De Cock <joost@joost.at> Date: Sun Jul 4 18:30:48 2021 +0200 wip(nextjs): Started work on freesewing.dev on NextJS
The mentioned workaround also seems to have solved this issue for me. Here's my config: webpack: (config) => {
config.module.rules.push({
test: /\.m?js$/,
type: 'javascript/auto',
resolve: {
fullySpecified: false,
},
});
return config;
}, I'm using .ts files with |
Thanks @graup, worked around the issue for me on Next 11.1.3. |
Sadly with next 14 its moved to rust compiler so if you want to take advantage of the new speeds, we'll need to find another work around. |
#58967 should already fix this I think, the workaround should no longer be required. |
Hi everyone! This seems to already be fixed on the latest, so I will be closing this one out! If you are still coming across this issue, please feel free to send us a new bug report. |
This closed issue has been automatically locked because it had no new activity for 2 weeks. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you. |
Bug report
Describe the bug
Importing a project ESM
.mjs
file (the Node.js standard file extension for ESM) in a page file causes a runtime error, but only in dev mode:Here is the call stack:
To Reproduce
Steps to reproduce the behavior, please provide code snippets or a repository:
Create a basic Next.js project.
Create a
config.mjs
file containing:In
pages/index.js
:In Terminal, run:
You should then see no error in Terminal, but on the client you will see the Next.js error overlay with the error described earlier.
Expected behavior
You should be able to use ESM
.mjs
files in your Next.js project without runtime errors in dev mode.System information
Additional context
Other Next.js users have also encountered this issue:
#13076
The text was updated successfully, but these errors were encountered: