# Event emitter for function object in separate module

**URL:** <https://discourse.nodered.org/t/event-emitter-for-function-object-in-separate-module/48850>\
**Category:** Developing Nodes\
**Created:** [24 July 2021 12:28 UTC](https://discourse.nodered.org/t/event-emitter-for-function-object-in-separate-module/48850 "2021-07-24T12:28:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)\
**Post date:** [24 July 2021 12:28 UTC](https://discourse.nodered.org/t/event-emitter-for-function-object-in-separate-module/48850/1 "2021-07-24T12:28:53Z")

</div>

Hi folks,

I have a function myFunction in separate someFile.js file:

```auto
var EventEmitter = require("events");

var MyFunction = function() {
    ...
    return this;
}

module.exports = MyFunction; 

```

I create an object of that function in another js file, and I want to attach an event handler to it:

```auto
const MyFunction = require('./lib/ParticleSwarmOptimizer');

var myFunctionObject = MyFunction();
myFunctionObject.on("someEvent", function() {...});

```

But I keep getting the error "_ **TypeError: myFunctionObject.on is not a function** _".

I have tried to do it like in [this](https://www.dev2qa.com/node-js-add-emit-event-feature-to-javascript-function-object-example/) tutorial, like in [this](https://programmersought.com/article/80113516355/) tutorial (section "Implement a custom class based on EventEmitter") and like in Node-RED's [Node.js](https://github.com/node-red/node-red/blob/master/packages/node_modules/%40node-red/runtime/lib/nodes/Node.js) file.

But I keep getting the same error over and over again. Still getting nuts from this kind of constructions in Javascript 🙄

Can somebody correct my code snippet to emit an event in the function, and listen to those events in the handler?

Thanks a lot !!!  
Bart

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [24 July 2021 12:45 UTC](https://discourse.nodered.org/t/event-emitter-for-function-object-in-separate-module/48850/2 "2021-07-24T12:45:25Z")

</div>

```auto
const EventEmitter = require('events').EventEmitter;

function MyEmitter() {
    if (!(this instanceof MyEmitter)) return new MyEmitter();
    EventEmitter.call(this);
    this.init();
}

inherits(MyEmitter, EventEmitter);

```

Usage...

```auto
const myem = require('./my-emitter');
let myEmitter = myem.MyEmitter();
myEmitter.on("event", function() {});

```

---

<div class="post-metadata">

**Author:** ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)\
**Post date:** [24 July 2021 20:28 UTC](https://discourse.nodered.org/t/event-emitter-for-function-object-in-separate-module/48850/3 "2021-07-24T20:28:13Z")

</div>

Hey Steve,  
It seems to work. Couldn't find it myself.  
At those moments I really miss the simple Java syntax ...

Thanks for sharing the code snippet!!

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [24 July 2021 22:14 UTC](https://discourse.nodered.org/t/event-emitter-for-function-object-in-separate-module/48850/4 "2021-07-24T22:14:30Z")

</div>

While this is good, I'm liking the singleton class approach myself at the moment. Works really nicely for slightly more complex requirements as embodied now in the web.js and socket.js modules in uibuilder.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [7 August 2021 22:15 UTC](https://discourse.nodered.org/t/event-emitter-for-function-object-in-separate-module/48850/5 "2021-08-07T22:15:06Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
