Search code examples
javascriptrequirejstypescriptosc

Importing OSC library using TypeScript AMD fails to build osc object correctly


I am trying to consume the browser portion of the OSC library here: https://github.com/colinbdclark/osc.js in my TypeScript AMD website (Using RequireJS.)

I intend to write the definition file at least to the extent that I use it. However, when I import the (minified or not) osc-browser js file via require:

        //From the require config
        oscbrowser: ["/scripts/lib/osc.js/dist/osc-browser.min"],
...
        oscbrowser: {
            export: "osc"
        },
...
//From the typescript file
declare var osc;

Requirejs gives me the following javascript error at runtime:

Uncaught Error: Module name "events" has not been loaded yet for context: _. Use require([])

I have updated to the latest library per Colin's comment below and the error changes to reference the slip library.

Uncaught Error: Module name "slip" has not been loaded yet for context: _. Use require([])

When I pull in all components via the require config like so:

    EventEmitter: ["/scripts/lib/eventEmitter/EventEmitter"],
    slip: ["/scripts/lib/slip.js/dist/slip.min"],
    osc: ["/scripts/lib/osc.js/src/osc"],
    osctransports: ["/scripts/lib/osc.js/src/osc-transports"],
    oscbrowser: ["/scripts/lib/osc.js/src/platforms/osc-browser"],

and EventEmitter: { export: "EventEmitter" }, osctransports: { deps: ["slip", "EventEmitter"] }, oscbrowser: { export: "osc", deps: ["slip","EventEmitter", "osc", "osctransports"] },

The error returns to the events error:

Uncaught Error: Module name "events" has not been loaded yet for context: _. Use require([])

If I change the line in osc-transports.js from

EventEmitter = EventEmitter || require("events").EventEmitter;

to

EventEmitter = EventEmitter || require("EventEmitter") || require("events").EventEmitter;

because I believe that the prior require only works with the NPM version of EventEmitter the javascript error goes away and the library appears to work.

Does anyone with more experience with RequireJS than me have an idea as to why I can't use just the dist/ocs-browser versions of the library?


Solution

  • I'm the creator of osc.js. Until recently, osc.js didn't directly support asynchronous module loaders. After seeing your question, I added support to the library for the universal module definition style boilerplate, so osc.js version 1.1.3 and above should work with Require.js now.