Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
oveddan opened this issue Sep 18, 2019 · 17 comments
Labels
API For function naming and other API questions. devops general high priority

Comments

@oveddan
Copy link
Contributor

oveddan commented Sep 18, 2019

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
Copy link

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
Copy link
Contributor

@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
Copy link
Contributor

@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
Copy link

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

@joeyklee joeyklee changed the title requiring ml5 in node.js results in error: ReferenceError: window is not defined [devOps / Node.js support] requiring ml5 in node.js results in error: ReferenceError: window is not defined Feb 19, 2020
@Prottoy2938
Copy link

Any solution yet?

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

@Prottoy2938
Copy link

Prottoy2938 commented Jul 13, 2020

@joeyklee , any solution?

@srcnalt
Copy link

srcnalt commented Aug 2, 2020

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

@joeyklee
Copy link
Contributor

joeyklee commented Aug 2, 2020

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
Copy link

srcnalt commented Aug 3, 2020

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
Copy link

scottie commented Mar 14, 2021

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

@joordonezo
Copy link

Any solution for this time? Thank you.

@SebastianBoehler
Copy link

Would love to see a solution for this.

@tlsaeger
Copy link
Member

@lindapaiste has been working on something check out #1318

@SebastianBoehler
Copy link

SebastianBoehler commented Mar 15, 2022

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
Copy link
Member

Any idea? @joeyklee @lindapaiste

@Megapixel99
Copy link

Still getting the error in version 0.12.2

@pramodjain
Copy link

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,
}
);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API For function naming and other API questions. devops general high priority
Projects
None yet
Development

No branches or pull requests