question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Use with Sveltekit: window is not defined

See original GitHub issue

Doing 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:closed
  • Created a year ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
mutewintercommented, Oct 2, 2022

Another solution to this problem is to remove the module-scoped usage of window.innerHeight and window.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 from import 'pixi-viewport'.

I patched my local pixi-viewport to remove the module-scoped dependence on window.

diff --git a/src/Viewport.ts b/src/Viewport.ts
--- a/src/Viewport.ts
+++ b/src/Viewport.ts
@@ -126,8 +126,8 @@
const DEFAULT_VIEWPORT_OPTIONS: ICompleteViewportOptions = {
-    screenWidth: window.innerWidth,
-    screenHeight: window.innerHeight,
+    screenWidth: 400,
+    screenHeight: 400,
    worldWidth: null,
    worldHeight: null,
    threshold: 5,
@@ -341,8 +341,8 @@ export class Viewport extends Container
      * @param {number} [worldHeight]
      */
     resize(
-        screenWidth: number = window.innerWidth,
-        screenHeight: number = window.innerHeight,
+        screenWidth: number = 400,
+        screenHeight: number = 400,
         worldWidth?: number,
         worldHeight?: number
     ): void

In my view, this fallback to window isn’t very useful and instead screenHeight / screenWidth should be required params.

0reactions
Donny-Hcommented, Sep 30, 2022

Solved with onMount(async () => { const { Viewport } = await import('pixi-viewport'); })

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found