title: Event emitter between files not working with ES6 import/export and worker_thread
body: <p>I have two files, first is <code>state.js</code> which you might see below:</p>
  <pre><code>import EventEmitter from &quot;events&quot;;
  import tasklist from 'tasklist';
  
  export const stateManager = new EventEmitter();
  
  //IIFE working every 10 seconds via cron task, from the file below
  (async () =&gt; {
    const processes = await tasklist();
    const exist = processes.find(({imageName}) =&gt; imageName === 'process.exe');
  
    if (!!exist) {
      console.log(!!exist)
      stateManager.emit('state', 1);
    } else {
      console.log(!!exist)
      stateManager.emit('state', 0);
    }
  })();
  </code></pre>
  <p>The second is the <code>eventListener.js</code> file, which running endlessly.</p>
  <pre><code>//import stateManager from first file
  import { stateManager } from &quot;./src/controller.js&quot;;
  
  /**
   * Here is part of the code
   * which starts the first file
   * as a Worker cron-task every 10 seconds
   * It doesn't block the thread.
   */
  const bree = new Bree({
    logger: logger,
    jobs: []
  });
  bree.add({
    name: &quot;state&quot;,
    interval: ms(&quot;10s&quot;)
  })
  bree.start();
  
  
  // First listener
  console.log('check');
  stateManager.on('state', function firstListener(...args) {
    console.log('State updated', args);
  });
  </code></pre>
  <p>After I launch the <code>eventListener.js</code>, the output which I see in the console is the following:</p>
  <pre><code>check
  false
  State updated [ 0 ]
   [LOGGER]  21-03-01 18:03:67  info : Worker for job &quot;state&quot; online
  false
   [LOGGER]  21-03-01 18:03:08  info : Worker for job &quot;state&quot; exited with code 0
   //every 10 second then =&gt;
   [LOGGER]  21-03-01 18:13:69  info : Worker for job &quot;state&quot; online
  false
   [LOGGER]  21-03-01 18:13:06  info : Worker for job &quot;state&quot; exited with code 0
  </code></pre>
  <p>or if I export <code>export const stateManager = new EventEmitter()</code> from another (third file) not the <code>state.js</code> directly.</p>
  <pre><code>check
   [LOGGER]  21-03-01 18:03:23  info : Worker for job &quot;state&quot; online
  false
   [LOGGER]  21-03-01 18:03:63  info : Worker for job &quot;state&quot; exited with code 0
   //every 10 second then =&gt;
   [LOGGER]  21-03-01 18:13:23  info : Worker for job &quot;state&quot; online
  false
   [LOGGER]  21-03-01 18:13:63  info : Worker for job &quot;state&quot; exited with code 0
  </code></pre>
  <p>So the task works fine, and the event listener never ends, but somehow, it doesn't react to <code>state</code> event or react only once, at the begining. Why such a problem could be? Since I have no other error in the console?</p>
  <blockquote>
  <p><em>If it helps, I use <code>bree.js</code> as a job queue manager, which actually runs the cron-task itself.</em></p>
  </blockquote>
  <h2>Solution</h2>
  <p>I guess I have found the solution to the problem, but I couldn't understand the reason itself.</p>
  <p>In my case, it's better to use not the <code>events</code> library, but <code>worker_threads</code>, so the code now looks like that:</p>
  <pre><code>import { parentPort } from &quot;worker_threads&quot;;
  import tasklist from 'tasklist';
  
  //IIFE working every 10 seconds via cron task, from the file below
  (async () =&gt; {
    const processes = await tasklist();
    const exist = processes.find(({imageName}) =&gt; imageName === 'process.exe');
    if (parentPort) parentPort.postMessage({ state: !!exist })
  })();
  </code></pre>
  <p>and the <code>eventListener</code> receive side looks like:</p>
  <pre><code>bree.on('worker created', (name) =&gt; {
    bree.workers[name].on('message', (message) =&gt; {
      console.log(message);  // Prints { state: !!exist }
    })
  });
  </code></pre>
  <p>So the messages from the first file have been delivered successfully.
  But I'll be very glad if someone, will explain it to me, why <code>events</code> library can't deliver messages between different works, even if the <code>eventListener</code> has been exported correctly.</p>