-
Notifications
You must be signed in to change notification settings - Fork 30.1k
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
ESM: exports not utilised when importing CJS. #29537
Comments
Also doesn't work with:
Despite docs using include and despite working for core which looks about the same (strange thing with default being set). |
@joeyhub sorry but I couldn't understand the problem here correctly. Can you share some code sample of your issue here? Thanks. |
Just set that in JSON. Even just running node and then require('@babel/traverse') needs me to do traverse.default. Both of these libraries do this:
Both also define the script as the main. |
What happens if you:
I get With one case I tried just assigning module.exports instead of just exports.default instead and it worked. |
@joeyhub its not a function, its an object. think of it as |
According to a google search, I think it's something to do with this:
Is the __esModule thing an old fallback prior to full ESM support to at least allow require to return a default as well as a nest of object entries? Did node ever support that or was it something people were doing and introducing an unnecessary transpile pass inadvertently for people that wanted to use the library straight up? If so do npm packages with an entry point like this need to be pointed out (they should otherwise dist with it compiled properly to cjs/es)? |
|
So basically I need to raise this with the NPM packagers for those libraries to fix their build and maybe push for it to be recognised as a problematic practice. |
i don't have a one-size-fits-all solution, but i will note that |
It's really problematic if you look for example at the babel manual. They have straight up ESM examples with none of that and no impression that you have to babel compile their library before you can use it in the way they specify (otherwise the ugly .default). Even is packages do use that function, I'm certain they're making a mistake at this point now with proper ESM not compiling fully to a CJS (main, legacy?) and ESM (module) entry point. If they were giving examples using as though that ESM style hack was in use before but not documenting to do that you need a whole transpile pass then I think they were also making a mistake even then (with a lot of users probably stumbling around to find .default or worse making an unnecessary build of something already meant to be built for you). If it was never supported by node then it should be gradually purged. |
Hello guys, @joeyhub Please confirm that issue is the same : https://github.com/Nainterceptor/node-issue-29537 ? Quick explain : |
I'm using type as module and mjs for the entry point (but after that I stick with js). I am noticing it appears to load the cjs (main) even though there's also a module in many cases though I have a tendency to get mixed up as well using ESM native locally but rollup to ship off the the browser. Maybe it is some inconsistent behaviour then and not just some annoyance with a stop gap getting in the way? I wonder if the resolve hook can bump it to the module version if present (probably not trivially / without repeating stuff, would probably want the loader hook but that's a bit of a mess right now). I wonder if this:
|
@Nainterceptor if you have i think this issue has been resolved at this point. |
@devsnek What ? In my code example, I don't want to load cjs, but cjs is loaded. |
@Nainterceptor node doesn't use the module field, there is too much code using it that is not compatible with native esm. |
@devsnek Thanks for the explaination ! So we've to use the main field, with files .js (or .cjs) and .mjs side-by-side, and let node choose the right one. |
this is what is works for me! |
Version: v12.10.0
Platform: msdos, x86_16
Subsystem: ESM
Using
"type": "module"
and an mjs as an entry point, module isn't uses from package.json and when falling back to main (CJS), using purely exports rather than assigning module.exports doesn't appear to work.Seen when trying to use rollup-plugin-analyzer.
The text was updated successfully, but these errors were encountered: