Skip to content
This repository has been archived by the owner on Jul 28, 2021. It is now read-only.

ReferenceError: window is not defined #24

Open
kandarppatel opened this issue Jun 9, 2020 · 8 comments
Open

ReferenceError: window is not defined #24

kandarppatel opened this issue Jun 9, 2020 · 8 comments

Comments

@kandarppatel
Copy link

We are trying to use this module with our React SSR (Server Side Rendering) project and its keep giving us below error.

ReferenceError: window is not defined
at Object.<anonymous> (/var/www/app/node_modules/tui-image-editor/dist/tui-image-editor.js:16:4)
at Module._compile (internal/modules/cjs/loader.js:778:30)
at Module._compile (/var/www/app/node_modules/pirates/lib/index.js:99:24)
at Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Object.newLoader [as .js] (/var/www/apcrat/admin/node_modules/pirates/lib/index.js:104:7)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Module.require (internal/modules/cjs/loader.js:692:17)
at Module.Hook._require.Module.require (/usr/local/lib/node_modules/pm2/node_modules/require-in-the-middle/index.js:70:39)

We have tried different solutions suggested online by setting window as global or conditioning in componentDidMount life cycle method but non of them working.

Is there any work around for this ?

@ravenaxiom
Copy link

I had to force the component to render client side to fix this problem - although it only solves it intermittently. I think the issue is something to do with the image editor trying to hook into the window object, which wouldn't be available until the code runs client side.

@swtalk
Copy link

swtalk commented Jul 31, 2020 via email

@Opty1712
Copy link

the same propblem
can't build React App on NextJS because of window in your js-bundle
image

@swtalk
Copy link

swtalk commented Dec 10, 2020 via email

@Logeshdas
Copy link

Next.js is universal, which means it executes code in the server-side first, then client-side. The window object is only present client-side, so it throws error like window is not defined. The below solution will overcome this error.

let ImageEditor;

import dynamic from 'next/dynamic';
if (typeof window !== 'undefined') {
ImageEditor = dynamic(() => import('@toast-ui/react-image-editor'),{ ssr: false })
}

export default function App(){
return ImageEditor;
}

@swtalk
Copy link

swtalk commented Dec 22, 2020 via email

@Opty1712
Copy link

@Logeshdas

thank you, but I know it
https://nextjs.org/docs/advanced-features/dynamic-import#with-no-ssr

Anyway it doesn't solve my problem, because I want make some unit tests on NodeJS and it's impossible with window budled inside((((((

@swtalk
Copy link

swtalk commented Dec 22, 2020 via email

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants