Use with Sveltekit: window is not defined
See original GitHub issueDoing a standard import:
import { Viewport } from 'pixi-viewport';
and get terminal error:
window is not defined ReferenceError: window is not defined
This is before doing anything else at all.
I know Sveltekit uses Vite. I’ve looked around for potential solutions but can’t come up with anything solid.
Any ideas?
Issue Analytics
- State:
- Created a year ago
- Comments:5 (1 by maintainers)
Top Results From Across the Web
SvelteKit: "Window is not defined" : r/sveltejs - Reddit
I am trying to use a library (toastr.js) in my SvelteKit application. But it seems like since SvelteKit is SSR by default (correct...
Read more >SvelteKit console error "window is not defined" when i import ...
I tried use a apexCharts after mount, but the error did not disappear. <script> import ApexCharts from 'apexcharts' import { onMount } from...
Read more >How To Solve "Window Is Not Defined" In SvelteKit - Joy of Code
You can use the onMount lifecycle function or the browser module from SvelteKit to make sure the code runs in the browser and...
Read more >ReferenceError: window is not defined #1503 - sveltejs/kit
Describe the bug After some time (from 1.0.0-next.87) I am using SvelteKit again. With 87 I can use console.log(window.localStorage) in $layout.svelte.
Read more >How To Deal With "Window Is Not Defined" In SvelteKit
What causes " window is not defined " in SvelteKit and how to fix it?
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Another solution to this problem is to remove the module-scoped usage of
window.innerHeight
andwindow.innerWidth
.The use-case is in hybrid SSR apps (like Next.js) you might want to use an export from Pixi Viewport (e.g. the
Viewport
TypeScript interface), but never actually render anything on the server. In that case, the server will crash just fromimport 'pixi-viewport'
.I patched my local
pixi-viewport
to remove the module-scoped dependence onwindow
.In my view, this fallback to
window
isn’t very useful and insteadscreenHeight
/screenWidth
should be required params.Solved with
onMount(async () => { const { Viewport } = await import('pixi-viewport'); })