magenta / magenta-js

Magenta.js: Music and Art Generation with Machine Learning in the browser
https://magenta.tensorflow.org
Apache License 2.0
1.97k stars 311 forks source link

[Node.js] "ReferenceError: performance is not defined" when using MusicVAE #471

Closed mxkrn closed 4 years ago

mxkrn commented 4 years ago

I'm running into this error when trying to initialize MusicVAE in Node.js; example code:

const mm = require('@magenta/music/node/music_vae');
const MODEL = 'https://storage.googleapis.com/magentadata/js/checkpoints/music_vae/groovae_2bar_humanize';

const grooveModel = new mm.MusicVAE(MODEL); 
grooveModel
    .initialize()
    .then(console.log('success!'))
    .catch(function(err) {
        console.log(err);
})

I suppose it makes sense as performance is a window function and is never imported from perf_hooks

adarob commented 4 years ago

Can you provide the full stack trace?

mxkrn commented 4 years ago
success!
ReferenceError: performance is not defined
    at O.initialize (/home/max/repos/M4L-Syncopate/node_modules/@magenta/music/node/music_vae.js:1:118473)
    at Object.<anonymous> (/home/max/repos/M4L-Syncopate/test/sandbox.spec.js:7:6)
    at Module._compile (internal/modules/cjs/loader.js:1138:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)
    at Module.load (internal/modules/cjs/loader.js:986:32)
    at Function.Module._load (internal/modules/cjs/loader.js:879:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
    at internal/main/run_main_module.js:17:47
adarob commented 4 years ago

I'm not so good at debugging js unfortunately. @notwaldorf or @cannoneyed do you have time to look into this?

cannoneyed commented 4 years ago

Yeah this is most likely an issue with using a global performance (which exists in the browser on the window object but not in Node.js)... It's used all over the place to measure load/inference times throughout the app so I'm a bit surprised that this isn't more pervasive of an issue. I'm happy to look into this a bit more deeply.

mxkrn commented 4 years ago

Awesome, thanks for addressing this so promptly!

notwaldorf commented 4 years ago

You’re also going to have some problems with fetch btw, which also is no bueno in Node. I have an example on how to shim it on the README: https://github.com/magenta/magenta-js/tree/master/music#in-node