node icon indicating copy to clipboard operation
node copied to clipboard

`NodeEventTarget` is not exported

Open Mesteery opened this issue 2 years ago • 9 comments

Subsystem

events

Additional information

Is it normal that NodeEventTarget is documented but not exported, nor global?

Mesteery avatar Aug 26 '21 18:08 Mesteery

Yeah, that's intentional. We really should decide what we want to do with it. /cc @addaleax

jasnell avatar Aug 26 '21 18:08 jasnell

May i ask why it was created in the first place? I'm not a huge fan of nodejs only features - like to write code that is more cross compatible...

jimmywarting avatar Aug 26 '21 20:08 jimmywarting

The intention here is to have a migration path for existing EventEmitter instances to become EventTarget instances, and to have the ability to use EventTargets in a way that is idiomatic for Node.js.

addaleax avatar Aug 27 '21 18:08 addaleax

No plans to exporting NodeEventTarget?

Semigradsky avatar May 09 '23 19:05 Semigradsky

Doesn't look like someone is working on a 'polyfill' for it either - https://www.npmjs.com/search?q=nodeeventtarget

However the only reason I wanted a NodeEventTarget is 'once', but addEventListener has a 'once' option too. with both signal and once options I wonder whether it's that useful

@Semigradsky what are you missing in EventTarget ?

unilynx avatar Feb 02 '24 10:02 unilynx

what are you missing in EventTarget ?

I just found this comment:

https://github.com/DefinitelyTyped/DefinitelyTyped/blob/eaaf5e44d4c874c4f7e38210cc62d053292f3b13/types/node/events.d.ts#L39-L41

Semigradsky avatar Feb 02 '24 13:02 Semigradsky

I think I actually wrote that comment, or at least the preface that your link points to -- the commented out interface likely came from existing code in the previous "events" module definition.

The core issue here is that the event module documentation is confusing. It says that NodeEventTarget is a "class", but as far as I can tell it's never exposed. The same is true of "classes" EventTarget and CustomEvent -- require('node:events') just returns the EventEmitter constructor.

There has been some discussion over on the DefinitelyTyped issue tracker about which "type-space" definitions should be exported by @types/node. For example, RequestInit is a "dictionary" defined in the Fetch spec -- there is no RequestInit defined at runtime, anywhere (browser or server), but Microsoft shipped a RequestInit interface in lib-dom and it's been generally useful to library authors, so it probably makes sense for Node to ship it as well, as part of fetch support.

That might apply here. @types/node could export the shape of NodeEventTarget as an interface, but at the moment that would be confusing because Node documents it as a class, and does not exist at runtime. Would it be helpful if the commented-out interface was just uncommented in @types/node so that you could import type {NodeEventTarget} from 'node:events'? If so, the DT maintainers might accept a PR -- just link to this issue as explanation.

Of course, that's not exactly what the OP was asking for. If there actually is a NodeEventTarget class / constructor-function which is internally maintained and used by Node, and somebody would find it useful to have the ability to invoke it or inherit from it, maybe they can speak up here?

thw0rted avatar Feb 02 '24 15:02 thw0rted

The same is true of "classes" EventTarget and CustomEvent

They're not exported by node:events but they're in the global namespace. extending from EventTarget and then using addEventListener and then dispatchEvent-ing a CustomEvent work for me in node

unilynx avatar Feb 04 '24 12:02 unilynx

Ah, that helps a lot, thanks. Apparently I misunderstood the docs -- they give the impression that each top level sidebar link describes a single module, and all the functions and classes in the corresponding page are exports from the module. Sometimes that's true, but sometimes they're talking about global entities that are used by / returned from the module's functions and classes. This probably isn't the issue to complain about that.

thw0rted avatar Feb 05 '24 15:02 thw0rted

The same is true of "classes" EventTarget and CustomEvent

They're not exported by node:events but they're in the global namespace. extending from EventTarget and then using addEventListener and then dispatchEvent-ing a CustomEvent work for me in node

Since which version?

This is the output for nodejs v18.18.2 and v20.10.0:

node -e 'console.log(typeof EventTarget)'
function

node -e 'console.log(typeof NodeEventTarget)'
undefined

piotr-cz avatar Mar 29 '24 18:03 piotr-cz

The same is true of "classes" EventTarget and CustomEvent

They're not exported by node:events but they're in the global namespace. extending from EventTarget and then using addEventListener and then dispatchEvent-ing a CustomEvent work for me in node

Since which version?

This is the output for nodejs v18.18.2 and v20.10.0:

I was talking about EventTarget, not NodeEventTarget. So apparently EventTarget is available on all versions that are still in support (18+)

Most of the things I personally might need from NodeEventTarget (Eg once) area already covered by the new options added to addEventListener.

unilynx avatar Mar 29 '24 19:03 unilynx