abdalla / react-hotjar

Small component to implement Hotjar into your react application
MIT License
165 stars 32 forks source link

window is not defined #10

Closed juliensl closed 5 years ago

juliensl commented 5 years ago

Hi!

I've this error when I'm trying to use react-hotjar :

W20190805-13:15:10.854(2)? (STDERR) ReferenceError: window is not defined
W20190805-13:15:10.854(2)? (STDERR)     at module.exports (/home/lfg-dev/PlatformCode/node_modules/react-hotjar/src/react-hotjar.js:14:5)
W20190805-13:15:10.854(2)? (STDERR)     at Object.initialize (/home/lfg-dev/PlatformCode/node_modules/react-hotjar/index.js:6:4)
W20190805-13:15:10.855(2)? (STDERR)     at init.js (packages/lfg-core/lib/server/init.js:6:23)
W20190805-13:15:10.855(2)? (STDERR)     at fileEvaluate (packages/modules-runtime.js:336:7)
W20190805-13:15:10.855(2)? (STDERR)     at Module.require (packages/modules-runtime.js:238:14)
W20190805-13:15:10.855(2)? (STDERR)     at Module.moduleLink [as link] (/home/lfg-dev/.meteor/packages/modules/.0.13.0.176lf51.fi71++os+web.browser+web.browser.legacy+web.cordova/npm/node_modules/reify/lib/runtime/index.js:38:38)
W20190805-13:15:10.855(2)? (STDERR)     at index.js (packages/lfg-core/lib/modules/index.js:1:244)
W20190805-13:15:10.855(2)? (STDERR)     at fileEvaluate (packages/modules-runtime.js:336:7)
W20190805-13:15:10.855(2)? (STDERR)     at Module.require (packages/modules-runtime.js:238:14)
W20190805-13:15:10.856(2)? (STDERR)     at Module.moduleLink [as link] (/home/lfg-dev/.meteor/packages/modules/.0.13.0.176lf51.fi71++os+web.browser+web.browser.legacy+web.cordova/npm/node_modules/reify/lib/runtime/index.js:38:38)
W20190805-13:15:10.856(2)? (STDERR)     at main.js (packages/lfg-core/lib/server/main.js:1:8)
W20190805-13:15:10.856(2)? (STDERR)     at fileEvaluate (packages/modules-runtime.js:336:7)
W20190805-13:15:10.856(2)? (STDERR)     at Module.require (packages/modules-runtime.js:238:14)
W20190805-13:15:10.856(2)? (STDERR)     at require (packages/modules-runtime.js:258:21)
W20190805-13:15:10.856(2)? (STDERR)     at /home/lfg-dev/PlatformCode/.meteor/local/build/programs/server/packages/lfg-core.js:4904:15
W20190805-13:15:10.856(2)? (STDERR)     at /home/lfg-dev/PlatformCode/.meteor/local/build/programs/server/packages/lfg-core.js:4909:3
W20190805-13:15:10.856(2)? (STDERR)     at /home/lfg-dev/PlatformCode/.meteor/local/build/programs/server/boot.js:419:36
W20190805-13:15:10.857(2)? (STDERR)     at Array.forEach (<anonymous>)
W20190805-13:15:10.857(2)? (STDERR)     at /home/lfg-dev/PlatformCode/.meteor/local/build/programs/server/boot.js:228:19
W20190805-13:15:10.857(2)? (STDERR)     at /home/lfg-dev/PlatformCode/.meteor/local/build/programs/server/boot.js:479:5
W20190805-13:15:10.857(2)? (STDERR)     at Function.run (/home/lfg-dev/PlatformCode/.meteor/local/build/programs/server/profile.js:510:12)
W20190805-13:15:10.857(2)? (STDERR)     at /home/lfg-dev/PlatformCode/.meteor/local/build/programs/server/boot.js:478:11

I'm using VulcanJS Framework, which is dealing with Node, React, Apollo, mongo and meteor. Do you have an Idea about how to solve this ?

Thank you very much. Julien.

abdalla commented 5 years ago

Hi, It seems you are using in the wrong place, to use hotjar you must have to have access to the window, maybe you are instantiating in a place you don't have a window.

juliensl commented 5 years ago

Mmmmm... It's weird, I'm instantiate it in my metaLayout, it should works.

I can print my window just before to initialize the module. The window is printed at the client side, but not at the server side, so I guess it's why the SSR does not working.

I have no idea where to solve that x)

jbsmith731 commented 5 years ago

@juliensl add inside of componentDidMount or useLayoutEffect

juliensl commented 5 years ago

It works ! Thank you @jbsmith731 .

You should write it in the doc, for stupid guys like me :rofl:

Have a good day and thank you again