-
Notifications
You must be signed in to change notification settings - Fork 27k
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
ERR_ESM_REQUIRE - Webpack 5 config should support ESM packages #23725
Comments
not the op, but reproducible with this. both
|
Thanks @stefanprobst I didn't see they'd requested a repro. :) |
Some background: @sindresorhus is in the process of moving most of his packages to ESM only in 2021. This error will likely become more common as more packages move to ESM only. I ran into the same issue with https://github.com/sindresorhus/slugify. Workaround: don't upgrade to latest major. |
workaround: use next-transpile-modules const withTM = require('next-transpile-modules')([
// ...
'p-debounce', // ESM module
]) |
I like @yurtaev's solution, but if anyone's relying on |
We have encountered the same issue with The solution for us was to add the following line to next.config.js:
This makes webpack treat UPDATE: no, this only made the build work. I saw a |
Same issue with new version of d3 modules like d3-scale |
A more permanent solution would be to Lines 140 to 142 in f18ce55
|
This still has the "please add a complete reproduction" tag, so I'm linking a minimal repo: https://github.com/JohnForster/esm-minimal-repo
|
For anyone running into similar problems ( @mario-jerkovic ?) that aren't immediately fixed with next-transpile-modules, you may need to include sub-dependencies in your next.config.js as well. I just ran into this problem with d3-delaunay but it might be present in other d3 modules as well. Keep an eye on the error messages to see which modules you need to add. My config looks like: // next.config.js
const withTM = require("next-transpile-modules")([
"d3-delaunay", // The package I'm trying to import
"delaunator", // A dependency of d3-delaunay
"robust-predicates", // A dependency of delaunator
]);
module.exports = withTM({
reactStrictMode: true,
}); |
Could the tag |
You can try the latest canary version and verify if it does work in your cases. |
@sokra I don't know if this is a bug or not, but when a pure ESM library imports a library that has "module" (non-standard ESM field) and "main" but not "exports", it fails. Is it planned to support non-standard fields? Or "exports" only? There is a large set of such libraries with "main" / "module" fields, so that's why I'm asking |
Do you have a name of such a package for reference? |
@sokra sure, here are some ("module" / "main" but no "exports"): |
it has been working for me with |
@frattaro can you make sure you're on |
Definitely am |
Can you provide a reproduction? Then we can have a look into it 👍 |
Creating the repro revealed my issue. Alone, using I have another package that uses
And at that point I stopped trying to fix it :) Edit: the package I'm importing is a UI framework, so it's going to have some edge cases |
another version of @frattaro I'm using
|
@exneval I'm trying to use it with Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: my-app\node_modules\unist-util-visit\index.js
require() of ES modules is not supported.
require() of my-app\node_modules\unist-util-visit\index.js from my-app\rehype\highlight-code.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from my-app\node_modules\unist-util-visit\package.json.
at new NodeError (node:internal/errors:363:5)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1126:13)
at Module.load (node:internal/modules/cjs/loader:989:32)
at Function.Module._load (node:internal/modules/cjs/loader:829:14)
at Module.require (node:internal/modules/cjs/loader:1013:19)
at require (node:internal/modules/cjs/helpers:93:18)
at Object.<anonymous> (my-app\rehype\highlight-code.js:4:15)
at Module._compile (node:internal/modules/cjs/loader:1109:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1138:10)
at Module.load (node:internal/modules/cjs/loader:989:32) {
code: 'ERR_REQUIRE_ESM' next.config.jsconst withTranspileModules = require('next-transpile-modules')([
'hast-util-to-html',
'hast-util-to-string',
'rehype-slug',
'remark-gfm',
'remark-parse',
'unified',
'unist-util-visit',
])
const withBundleAnalyzer = bundleAnalyzer({
enabled: process.env.ANALYZE === 'true',
})
module.exports = withTranspileModules(withBundleAnalyzer(nextConfig)) I even tried changing my custom
Do I need to do anything else? |
@deadcoder0904 maybe setup issue? I'm using next compose plugins to wrap the transpile, This config, for latest
or maybe you can try NextJS 11.1.0 , and try
no need to use transpile anymore, finally NextJS support it, and it become default in NextJS 12 |
@exneval I thought I needed to use even You also don't need to use |
@deadcoder0904 can you provide a reproduction repository so that we can have a look? |
@timneutkens take a look here -> https://github.com/deadcoder0904/next-esm-error Install & run to see errors :) |
@deadcoder0904 |
@stefanprobst I did actually, just not in the repro. Pushed a new commit but still error :) |
@deadcoder0904 Your problem is unrelated to next.js/webpack processing your files. You are trying to This happens while loading the next.config.js. Sadly next.js |
@sokra but I can't
I already said this above :)
Got it. So I cannot use ESM till Edit: 13 hours later, came back to the same issue & looks like this is the issue I should be subscribed to :) |
Failed to require remark-math. I have to import remark-math in next.config.js . It gives this error when trying to import. I am on next@canary. Error: failed to load next.config.js, see more info here https://nextjs.org/docs/messages/next-config-error |
I'm guessing the use-case for importing The whole |
Yes, that is what I am thinking of doing. |
This worked perfectly for me. Using |
|
@Prakhar-FF13 by any chance, did you manage to setup math display in MDX? |
No I used mdx bundler by Kent c Dodds. |
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.1.3
What version of Node.js are you using?
14.x
What browser are you using?
Chrome
What operating system are you using?
macOS
How are you deploying your application?
Vercel
Describe the Bug
p-queue
recently updated to using ESM modules, and it looks like some webpack configuration is required to support it. I doubt this is the last time this particular issue will come up.Expected Behavior
Users should be able to require ESM modules without getting an error.
To Reproduce
Install
[email protected]
, import it, and try to start next.Thanks!
The text was updated successfully, but these errors were encountered: