ml5js / ml5-library

Friendly machine learning for the web! 🤖
https://ml5js.org
Other
6.48k stars 902 forks source link

[devOps / Node.js support] requiring ml5 in node.js results in error: ReferenceError: window is not defined #570

Open oveddan opened 5 years ago

oveddan commented 5 years ago

I have a basic program in node.js, where I run:

npm install ml5

This installs ml5 version 0.3.1

When I try to require it in my program with:

cont ml5 = require('ml5');

I get an error:

(function (exports, require, module, filename, dirname) { !function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.ml5=e():t.ml5=e()}(window,function(){return function(t){var e={};function n(r){if(e[r])return e[r].exports;var i=e[r]={i:r,l:!1,exports:{}};return t[r].call(i.exports,i,i.exports,n),i.l=!0,i.exports}return n.m=t,n.c=e,n.d=function(t,e,r){n.o(t,e)||Object.defineProperty(t,e,{configurable:!1,enumerable:!0,get:r})},n.r=function(t){Object.defineProperty(t,"esModule",{value:!0})},n.n=function(t){var e=t&&t.esModule?function(){return t.default}:function(){return t};return n.d(e,"a",e),e},n.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},n.p="/",n(n.s=698)}([function(t,e,n){"use strict";n.r(e),function(t,r,i,a){n.d(e,"AdadeltaOptimizer",function(){return ml}),n.d(e,"AdagradOptimize ReferenceError: window is not defined at Object. (/Users/danoved/Source/in_class_demos/puppeteer_lstm/node_modules/ml5/dist/ml5.min.js:1:250) at Module._compile (module.js:652:30) at Object.Module._extensions..js (module.js:663:10) at Module.load (module.js:565:32) at tryModuleLoad (module.js:505:12) at Function.Module._load (module.js:497:3) at Module.require (module.js:596:17) at require (internal/module.js:11:18) at Object. (/Users/danoved/Source/in_class_demos/puppeteer_lstm/test_chat.js:2:13) at Module._compile (module.js:652:30)

samuelrosacv commented 5 years ago

Tenho o mesmo problema. Estou estudando node.js, tensorflow, ml5, etc... pelo que entendi, esse framework ml5 foi criado para rodar diretamente no browser. Tanto é que o objeto BOM window é invocado dentro do ml5.min.js. Se criar um js e rodar a partir de um html vai funcionar.

joeyklee commented 5 years ago

@oveddan - Thanks for raising this issue. I've added this to the list of todos for the next coming weeks/months. I'd love for ml5 to be more flexible in this regard.

Right now we have issues with supporting ml5 as a npm module and can only really be used via the CDN link. This has to do with calls to the window object that I think might be causing any bundling to break.

You've also mentioned how awesome it would be to be able to choose specific ml5 features (e.g. to be able to do npm install ml5/bodypix). This is a super nice suggestion and am all for it.

If you've got ideas for how we might move this forward, I'm all ears and happy to support.

In the meantime, we should def. do our best to make sure ml5 doesn't break in other environments!

Thank you!

joeyklee commented 5 years ago

@samuelrosacv muito obrigado pra sua messagem. (meu portugeus nao e bom, mas posso falo um poco). Nos esperamos resolver esse "issue" estes proximos semanas.

Obrigadao!

lieberscott commented 4 years ago

Would love if you could please get it working with Node as well. Thanks!

Prottoy2938 commented 4 years ago

Any solution yet?

I'm using Nextjs, React and ml5 (v - 0.5) node module and the window error is still here

Prottoy2938 commented 4 years ago

@joeyklee , any solution?

srcnalt commented 4 years ago

In Next.js project with import * as ml5 from 'ml5' same issue.

joeyklee commented 4 years ago

Hi @Prottoy2938 and @srcnalt - Thanks for your comments here. The changes required here are on our roadmap, but I can't say for sure when this will be resolved. I wish I could give you a better timeline for this, but for the time being there has not been any progress here.

The way ml5 is structured at the moment makes calls the window that cause it to fail in the contexts you're attempting to use the library in. Sorry!

srcnalt commented 4 years ago

Thank you for the reply @joeyklee meanwhile I was able to get it working using react hooks, hope it helps others who have the same issue.

//global
let ml5: any

//in component
useEffect(() => {
  ml5 = require('ml5')
}, [])
scottie commented 3 years ago

well its like two years later, guess you never really managed to look into it.

joordonezo commented 3 years ago

Any solution for this time? Thank you.

SebastianBoehler commented 2 years ago

Would love to see a solution for this.

tlsaeger commented 2 years ago

@lindapaiste has been working on something check out #1318

SebastianBoehler commented 2 years ago

Still getting the error on v0.10.5 ReferenceError: window is not defined at Object.<anonymous> (C:\Users\Basti\Documents\GitHub\ml5_regression_project\node_modules\ml5\dist\ml5.min.js:1:188)

tlsaeger commented 2 years ago

Any idea? @joeyklee @lindapaiste

Megapixel99 commented 2 years ago

Still getting the error in version 0.12.2

pramodjain commented 10 months ago

I was using ml5 with nextjs and was getting the same error, I resolved this issue by dynamically importing the component which I was rendering in nextJS pages.

const MyComponent= dynamic( () => import("../components/my-component"), { ssr: false, } );