-
Notifications
You must be signed in to change notification settings - Fork 27.2k
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
Nextjs fails to detect ESM modules correctly when using exports in package.json #39375
Comments
After some more testing the .mjs extension seems to cause issues, renaming the esm files to .js resolves the issues. |
Thanks, I was first going to suggest upgrading the packages to When ESM resolution is enabled, the work is handed off to the Node.js resolver:
Given that All examples I have seen define both Webpack's guidelines also suggest avoiding
Resources: |
Thanks @balazsorban44 This issue is related to: chakra-ui/chakra-ui#6436 Chakra UI uses .js extension for ESM modules. Any other package that depends on Chakra UI and uses .mjs extension for ESM modules will run into this error. Renaming all .mjs files to .js in @saas-ui/react ultimately resolved it for now, but this is more a workaround then a solution imho. I looked into framer as well, which had a similar issue open with Next.js last year. They are using require (.js) and default (.mjs) without any issues, so that doesn't seem to be the problem. Here to proposed solution was to use .mjs. I'm not sure what is going on, but anyway the compiler behaviour changes based on which extension is used for ESM modules, while the package.json definitions are the same, as well as the code of the modules. |
As far as I can tell, Framer does not have "type": "module",
"exports": {
".": "./build/index.js",
"./package.json": "./package.json",
"./*": "./build/*"
}, |
Sorry, probably the wrong package above. "exports": {
".": {
"require": "./dist/cjs/index.js",
"import": {
"types": "./dist/index.d.ts",
"default": "./dist/es/index.mjs"
},
"default": "./dist/cjs/index.js"
},
"./package.json": "./package.json"
}, https://unpkg.com/browse/framer-motion@7.0.0/package.json So actually they use both |
Ok, my bad I misread this yesterday. Then we can agree using both 'import' and 'require' is the way to go, leaves us only with the question why the compiler behaves different with .js and .mjs file extensions for es modules. |
Do you mean saas-js/saas-ui@a43204d? If you want to signal Node.js to treat your
https://nodejs.org/api/packages.html#type UPDATE: Oh, it's the other way around. 🤔 You fixed it by going from Also, If using
|
So if no .mjs are found (even though type = module , or 'import' is defined, it will always treat the package as cjs? |
1 similar comment
This comment was marked as duplicate.
This comment was marked as duplicate.
Not sure yet, will have to investigate further. |
Our research would confirm this. We have the following setup: NextJS project that uses
{
"exports": { ".": { "import": "./icons-react/dist/index.esm.js" } }
} Now the above ends with Now what NextJS does is: If THAT package is requested from ANOTHER package (so not directly from the NextJS app but from a sub-package, in our case This is extremely weird and I'd be willing to help out here if I know in which file to start. I am actually wondering why so few people seem to have this issue. But I assume that people find workarounds. Our findings are backed by the fact that if we rename those files to |
I've done a bit more research on this and since the error is being triggered with this error log
I have the gut feeling that this problem might be rather a node-based problem than a framework-based problem which is interesting. |
According to https://nodejs.org/api/esm.html So maybe at this point of information / node status it's best to have this resolved in the related / imported libraries. But what's really interesting is why it works for direct dependencies (maybe because NextJS bundles those but it doesn't transform the sub-deps?) |
Alright. I've researched this for quite some while now. What I reported above seems to hold true. Now for people searching for a SOLUTION only I have exactly 2 proposals for you:
const nextConfig = {
experimental: {
transpilePackages: ["problematic-package", "other-problematic-package", "etc", "..."],
},
} Warning: This isn't a clean solution so only do this when you are beyond desperate. |
@activenode Great work, thank you. The saga continues for me. Chakra UI renamed everything back to .mjs, and my library started having the same issues again, but reversed :) I've released new bundles with .mjs extensions, which fixed it partially. But one dependency @react-aria/* uses .js for module bundles, so back at the start.
I was wondering the same, looking at the logs nothing really points to Next.js Also Webpack detects the packages correctly as ESM, even overriding the package type to ESM doesn't seem to work.
|
For reference. Adobe reverted their update to .mjs recently. |
Well, there's just standard people working everywhere. But with revert you mean Which would make sense to me. I get your pain and I feel it comes from the mix (which must be resolved depending on how the mix is setup - which is one of the described problems here). At the end of the day it comes down to: If everything is using What I still find interesting though is that it only appears (in my experience) with deps of deps not with deps but that could be explained as direct deps might run through the bundler whereas the deps of deps don't. |
I was having the same problems with a
to:
as you may have noticed it also requires renaming
|
Is |
Too few information: When does this help and where? Are you suggesting to set |
Facing the same issue when bumping nextjs from It still works in error - file:///Users/marco/code/blog/node_modules/next-contentlayer/dist/hooks/useLiveReload.js:1
import { addMessageListener } from 'next/dist/client/dev/error-overlay/websocket.js';
^^^^^^^^^^^^^^^^^^
SyntaxError: Named export 'addMessageListener' not found. The requested module 'next/dist/client/dev/error-overlay/websocket.js' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:
import pkg from 'next/dist/client/dev/error-overlay/websocket.js';
const { addMessageListener } = pkg;
at ModuleJob._instantiate (node:internal/modules/esm/module_job:124:21)
at async ModuleJob.run (node:internal/modules/esm/module_job:190:5) {
digest: undefined
} Any thoughts?
|
@marcofranssen having this issue too with 13.2.4 now with a fresh checkout of my project. I had to upgrade @swc/helpers in order to resolve another bug. #48593 Seems to related to swc's resolver? |
Fixed it with those two steps
Looks like this step isn't needed. but feel free to change it if you like.
I'm on next 13.1.2 |
Thank you very much, it works for me |
Pálmi Þór Valgeirsson is reporting the following import failure on Vercel with the Bun runtime: [11:48:09.012] Error [ERR_REQUIRE_ESM]: require() of ES Module /vercel/path0/node_modules/node-fetch/src/index.js from /vercel/path0/node_modules/@libsql/isomorphic-fetch/node.cjs not supported. [11:48:09.012] Instead change the require of index.js in /vercel/path0/node_modules/@libsql/isomorphic-fetch/node.cjs to a dynamic import() which is available in all CommonJS modules. This seems to be a known issue on Vercel: vercel/next.js#39375 With the suggested workaround of: > After some more testing the .mjs extension seems to cause issues, renaming the esm files to .js resolves the issues.
I have tested this out on a repo by copying the new dist into the node_modules and can confirm this solves the issues that were happening with builds for next. see vercel/next.js#39375 for more info
I have tested this out on a repo by copying the new dist into the node_modules and can confirm this solves the issues that were happening with builds for next. see vercel/next.js#39375 for more info
What worked in my case was to remove the curly braces from the import statement in the quoted JS file: "/var/www/nextjs/node_modules/next-contentlayer/dist/hooks/useLiveReload.js" info - Collecting page data .file:///var/www/nextjs/node_modules/next-contentlayer/dist/hooks/useLiveReload.js:1 |
Verify canary release
Provide environment information
What browser are you using? (if relevant)
No response
How are you deploying your application? (if relevant)
No response
Describe the Bug
Next.js fails to resolve sub dependencies correctly and tries to load CJS from ESM modules, causing the build to fail.
@chakra-ui/react a dependency of @saas-ui/react
Saas UI uses exports definition in package.json to define package entries and exports the ESM module as default.
Removing exports or changing default to
import
fixes the issue.However I think Next.js should handle this correctly.
Expected Behavior
Next.js resolves all packages correctly as ESM.
Link to reproduction
https://github.com/msnegurski/test-saas-ui
To Reproduce
Make sure @saas-ui/react@1.2.x is installed (not 1.3)
NEXT-1381
The text was updated successfully, but these errors were encountered: