Closed jjeff closed 2 years ago
With version 3, Node.js support was added to the library. When the library is run within the Node environment, the jzz
module is imported because it provides the missing Web MIDI API.
However, as you are pointing out, some environments may look like both Node.js and the browser. Electron is one but it's probably the same for NW.js and React Native.
In any case, your suggestion makes perfect sense, but it would have to be something like that to prevent an error being thrown:
if (Utilities.isNode) {
try {
window.navigator;
} catch (err) {
let jzz;
eval('jzz = require("jzz")');
global["navigator"] = jzz;
}
}
Would you be able to test the above code in Electron? Or, perhaps you could provide a basic Electron example that I could try? I actually want to add an Electron example in the examples
directory.
P.S. I would prefer not to overwrite the whole navigator
property but the jzz
module does not leave me much choice because it creates and uses many objects in the navigator
namespace.
Can you try v3.0.10 and tell me if it helps?
It helps! No more error. Looks like things are working now.
Migrating my Electron-based app from WebMidi 2 to WebMidi 3, I get this error in the renderer console when attempting to boot my app:
I'm guessing this is related to this code at the top of WebMidi.js:
Maybe I'm misunderstanding, but it looks like this code says: "If we're running Node, then we must not be in a browser window, so we can create our own navigator object." However, in the case of Electron, that's a false assumption. Electron can... and often does run Node in the browser window - or the "renderer" process as Electron calls it. So attempting to redefine the navigator property of the global object (aka
window
in the renderer) produces an error and the renderer fails.Again, I'm just guessing, but maybe this code should say something more like: