Skip to content
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

Make it more ESM module friendly #281

Closed
miguelcobain opened this issue Jan 8, 2022 · 4 comments
Closed

Make it more ESM module friendly #281

miguelcobain opened this issue Jan 8, 2022 · 4 comments

Comments

@miguelcobain
Copy link
Contributor

miguelcobain commented Jan 8, 2022

Right now, with ESM modules, we can't do this:

import { EventEmitter2 } from 'eventemitter2';

and instead have to do this:

import pkg from 'eventemitter2';
const { EventEmitter2 } = pkg;

This is a great blog post that explains the issue: https://simonplend.com/node-js-now-supports-named-imports-from-commonjs-modules-but-what-does-that-mean/

Is there anything we can do to make this library more friendly to modern module specs?
Ideally EventEmitter2 would be a default export and not a named export.

@RangerMauve
Copy link
Contributor

Is this using the ESM integration within Node.js or is this part of the functionality of a bundler like webpack?

@miguelcobain
Copy link
Contributor Author

This is part of Node.js, yes, no bundler needed.

@RangerMauve
Copy link
Contributor

I think EventEmitter2 initially got exported the way it has due to how Node.js' built-in events package worked. Would you be able to look into how that's been working in ESM and to see if we could do something similar here?

I'd be a bit worried about backwards compat, but maybe we could release a new major version for just those folks that want it. 🤷 Alternately, could we have a separate file for ESM imports that imports the commonjs module and then gives more appropriate ESM exports?

@danilofuchs
Copy link

This issue is not fixed.

Adding type definitions only tricks Typescript into thinking it works, when in reality the import breaks during runtime, as Node.js cannot find the EventEmitter2 named export.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants