RubenVerborgh/AsyncIterator

.mjs module fails in Webpack

Vinnl opened this issue · 11 comments

Vinnl commented

I haven't completely gotten to the bottom of this yet, but I think the problem lies in this package - so reporting in case it rings a bell for you.

When I generate a new app using Create React App, then import a module that brings in AsyncIterator (@solid/query-ldflex, in this case), I get the following error:

./node_modules/asynciterator/asynciterator.mjs
Can't import the named export 'EventEmitter' from non EcmaScript module (only default export is available)

Named imports of native Node modules might work in Node with ES Modules enabled, but it seems to be causing problems when trying to do so from Webpack's mock.

Yeah, so we’re using the new module field from Node. But not all tooling seems to be ready for it. Can you force webpack to use the .js instead of .mjs (both are included)? On mobile now, but the event thing is a known issue with Node/webpack.

Might need to wait with using the module field, and release a new version that doesn’t. Also looping in @rubensworks.

Yeah, we ran into the same problem in Comunica. This webpack config provides a workaround: https://github.com/comunica/comunica/blob/master/packages/actor-init-sparql/webpack.config.js#L20-L23

Vinnl commented

Isn't Node's field export? In other words, would it be possible to have module point to the .js, and use exports for the .mjs? Having to modify the Webpack config is quite a big ask of people using a metaframework like Create React App or Next.js, which otherwise handles that config for you.

export indeed, but the whole point of module is to be an ES6 module. I’m also not in favor of having to modify webpack configs; maybe I can either remove the whole module thing, or provide a separate module for webpack with the event thing fixed.

Vinnl commented

Either would be great!

is there any chance of a quick fix? I'm running into the same problem. I really would like to use @comunica/actor-init-sparql in my react-app, but get the same mentioned error.

Fix coming in the next couple of days.

Based on @rubensworks' fix, I have published v3.0.1 where the .mjs extension is no longer used.

Vinnl commented

Thanks @rubensworks and @RubenVerborgh! I can confirm that I can now load @solid/query-ldflex into a CRA app again.

Super, thanks for reporting and the quick test!

For future reference: the .mjs/.cjs strategy came from https://nodejs.org/docs/latest-v14.x/api/esm.html#esm_writing_dual_packages_while_avoiding_or_minimizing_hazards, which is thus not a good idea to follow at this point in time.