0

I'm new to require.js and trying to use RosLib.js on my page by loading it with require.js. RosLib.js has a dependency to EventEmitter2.

So this is my code:

require.config({
    shim: {
        eventemitter: {
            exports: 'EventEmitter2'
        },
        roslib: {
            deps: ["eventemitter"],
            exports: "ROSLIB"
        }
    },
    paths: {
        roslib: "https://raw.github.com/RobotWebTools/roslibjs/devel/build/roslib",
        eventemitter: "https://raw.github.com/hij1nx/EventEmitter2/master/lib/eventemitter2"
    }
});

require(["roslib"], function (ROSLIB) {
    var urlname = "ws://" + location.hostname + ":9090";
    ros = new ROSLIB.Ros({
        url : urlname
    });
});

If the function Is executed somhow eventemitter2.js has an error and EventEmitter2 isn't defined:

Uncaught ReferenceError: module is not defined (eventemitter2.js:561)

Uncaught ReferenceError: EventEmitter2 is not defined (roslib.js:121)

Here is the corresponding example JsFiddle which isn't working: http://jsfiddle.net/mKyEA/1/

How do I have to configure require.js to properly initialize EventEmitter2?

Stefan Profanter
  • 6,458
  • 6
  • 41
  • 73

2 Answers2

3

It looks like you are doing requirejs correctly.

It seems the EventEmitter2.js you specified is the problem... That script alone throws an error. "module not defined". That variable is clearly not defined in the bottom of its script.

I grabbed a different version of EventEmitter2 off the net and customized it to expose the global variable EventEmitter2 so that ROSlib can see it.

I am not familiar with ROSlib but it seems to be missing a function setMaxListeners. Because after putting in the new EventEmitter2 I am getting the following error. "TypeError: this.setMaxListeners is not a function"

Jsfiddle was giving me some weird errors. This code should be what your looking for though. I attached a pastebin for convenience. pastebin example It shows fixed event emitter. http://jsfiddle.net/mKyEA/6/

    require.config({
    shim: {
        eventemitter: {
            exports: 'EventEmitter2'
        },
        roslib: {
            deps: ["eventemitter"],
            exports: "ROSLIB"
        }
    },
    paths: {
        roslib: "https://raw.github.com/RobotWebTools/roslibjs/devel/build/roslib",
        eventemitter: "http://yourjavascript.com/15010010093/eventemitter2"
    }
});
TheProdigy
  • 66
  • 3
  • Thanks for your help! Where did you grab your EventEmitter2 version? Because it seems it is quite old. The 'setMaxListeners' is called from EventEmitter2 and it seems your version doesn't contain this function. (For the original function see here: https://github.com/hij1nx/EventEmitter2/blob/236b78f362050876204be3047c12f28f693a993a/lib/eventemitter2.js) – Stefan Profanter Oct 14 '13 at 18:57
  • Thanks! Now it seems to work fine. I don't know what exactly you changed... Can you create a pull request to this issue with your changes: https://github.com/hij1nx/EventEmitter2/issues/100 Would also help others!! Thanks a lot. – Stefan Profanter Oct 14 '13 at 19:47
1

There were two problems:

The first problem was in the EventEmitter2 Library. I got the Uncaught ReferenceError: module is not defined (eventemitter2.js:561) exception.

This exception should be fixed by this commit: https://github.com/Pro/EventEmitter2/commit/f829a2571b4adc66d304cb9fd5a2a5698d41c107

The next problem is that RosLib doesn't find EventEmitter: Uncaught ReferenceError: EventEmitter2 is not defined (roslib.js:121)

I fixed this by adding an additional require setting the global EventEmitter2. Because RosLib expects EventEmitter2 to be global but since require.js is used it isn't set on windows.EventEmitter2:

require(["eventemitter", ], function (EventEmitter2) {
    window.EventEmitter2 = EventEmitter2;
});

Here is the new and working fiddle:

http://jsfiddle.net/43dCV/1/

Stefan Profanter
  • 6,458
  • 6
  • 41
  • 73