primus / eventemitter3

EventEmitter3 - Because there's also a number 2. And we're faster.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Circular dependency when bundling

renataogarcia opened this issue · comments

When bundling with rollup I get the following warning:

(!) Circular dependency
../node_modules/eventemitter3/index.mjs -> ../node_modules/eventemitter3/index.mjs

Then when trying to run the output I get the following error:

const EventEmitter = EventEmitter;
                     ^

ReferenceError: Cannot access 'EventEmitter' before initialization

It works fine up to version 4.0.7

Rollup resolves the import from index.js as index.mjs which causes the circular problem.

By renaming the file to wrapper.mjs instead (same as in the node docs) I was able to get it working.

I cannot reproduce the issue:

$ cat main.js 
import EventEmitter from 'eventemitter3';

const ee = new EventEmitter();

ee.on('foo', function () {
  console.log('Ok');
});

ee.emit('foo');
$ cat rollup.config.mjs 
import commonjs from '@rollup/plugin-commonjs';
import nodeResolve from '@rollup/plugin-node-resolve';

export default {
  input: './main.js',
  output: {
    file: 'bundle.mjs',
    format: 'es'
  },
  plugins: [commonjs(), nodeResolve()]
};
$ npx rollup -c

./main.js → bundle.mjs...
created bundle.mjs in 76ms
$ node bundle.mjs 
Ok

Thanks for looking into this.

I investigated why it's only happening in my case, and it seems that there's an issue with rollup-plugin-swc3 resolution which is causing this problem.