Skip to content

[EventEmitter] TypeError when calling .listeners() without arguments #56263

Description

@origranot

Version

22.12.0

Platform

Darwin X.local 24.1.0 Darwin Kernel Version 24.1.0: Thu Oct 10 21:02:45 PDT 2024; root:xnu-11215.41.3~2/RELEASE_ARM64_T8112 arm64

Subsystem

No response

What steps will reproduce the bug?

const EventEmitter = require('node:events');

const myEmitter = new EventEmitter();

try {
    myEmitter.listeners()
} catch (error) {
    console.error("Error: ", error.message);
    //Error: TypeError: Cannot read properties of undefined (reading 'length')
}

How often does it reproduce? Is there a required condition?

The issue reproduces 100% of the time when .listeners() is called without any arguments. There are no special conditions required - it happens consistently on any EventEmitter instance.

This issue could have a widespread impact. Many applications and libraries that extend or use a custom EventEmitter might be affected when they upgrade to the latest Node.js version.

What is the expected behavior? Why is that the expected behavior?

The expected behaviour is to return an empty array.

What do you see instead?

Error: TypeError: Cannot read properties of undefined (reading 'length')
    at arrayClone (node:events:894:15)
    at unwrapListeners (node:events:905:15)
    at _listeners (node:events:803:5)
    at EventEmitter.listeners (node:events:813:10)
    at Object.<anonymous> (/Users/ori.granot/Git/events-node-22/main.js:6:15)
    at Module._compile (node:internal/modules/cjs/loader:1562:14)
    at Object..js (node:internal/modules/cjs/loader:1708:10)
    at Module.load (node:internal/modules/cjs/loader:1318:32)
    at Function._load (node:internal/modules/cjs/loader:1128:12)
    at TracingChannel.traceSync (node:diagnostics_channel:322:14)```

Additional information

It looks like it was changed in the latest release:
bdb6d12e7a#diff-330bdd22a188135540523f69deb4f9563e03b00a913cd369e1ee84d899f178a9L785

I created a PR to fix the issue: #56264

Activity

  1. noamski commented on Dec 15, 2024

    @noamski

    Also encountered this issue, a fix will be great

  2. changed the title [-][EventEmitter] TypeError when calling .listeners() without arguments (22.12.x)[/-] [+][EventEmitter] TypeError when calling .listeners() without arguments[/+] on Dec 16, 2024
  3. jakecastelli commented on Dec 16, 2024

    @jakecastelli
    Member

    Good finding, this regression starts to appear on 22.12 and 23.3

  4. added
    eventsIssues and PRs related to EventEmitter and the events module.
    confirmed-bugIssues and PRs for confirmed bugs.
    on Dec 16, 2024
  5. lpinca commented on Dec 19, 2024

    @lpinca
    Member

    Fixed by acda05d.

  6. Betalos commented on Jan 20, 2025

    @Betalos

    @lpinca The fix you mentioned didn't land in node 22, was there a reason ?

  7. jakecastelli commented on Jan 20, 2025

    @jakecastelli
    Member

    This should be backported soon to 22.x - #56282

  8. mark-wiemer commented on Jul 28, 2025

    @mark-wiemer

    Will this be backported to Node 20? It was not present in 20.18.3 but was introduced in 20.19.0 and is still present in 20.19.4

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    confirmed-bugIssues and PRs for confirmed bugs.eventsIssues and PRs related to EventEmitter and the events module.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions