`new nativeeventemitter()` was called with a non-null argument without the required `addlistener` method.

When the new NativeEventEmitter() constructor is called with a non-null argument that does not have the required addListener method, an error occurs. The addListener method is essential for registering event listeners on the emitter object.

Here’s an example to illustrate this:


// Define a custom class without the required addListener method
class MyEmitter {
  constructor() {
    // ...
  }

  // Some other methods of the custom class
  // ...
}

// Create a NativeEventEmitter instance with the custom class as argument
const emitter = new NativeEventEmitter(new MyEmitter());

In this example, “MyEmitter” is a custom class that does not have the required “addListener” method. When trying to instantiate a “NativeEventEmitter” object with the “MyEmitter” object as an argument, an error will be thrown because the required method is missing.

To resolve this issue, you need to ensure that the argument passed to the “new NativeEventEmitter()” constructor is an object that implements the necessary “addListener” method. For example, you could use the built-in “EventEmitter” class from the “events” module in Node.js:


const EventEmitter = require('events');

// Create an instance of the EventEmitter class
const emitter = new NativeEventEmitter(new EventEmitter());

In this updated example, the “EventEmitter” class is used, which does have the required “addListener” method. Now the “NativeEventEmitter” object can be successfully instantiated without any errors.

Read more

Leave a comment