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.

"window is undefined" in nextjs

See original GitHub issue
  • FYI: going forward this will likely continue to be an issue due to the popularity of nextjs.
  • The root cause is that pixi does not work in node.js so it blows up when run in SSR.
  • This is related to an old bug with pixi.js v4: https://github.com/pixijs/pixi.js/issues/3224
  • The workaround is to use dynamic imports to remove it from the SSR phase. See https://medium.com/frontend-digest/why-is-window-not-defined-in-nextjs-44daf7b4604e
  • Unfortunately, this is not an ideal solution.
  • The use case is a React component that uses pixi.js under the hood. https://gitlab.com/robotandkid/awesome-react-cutscenes
  • Because of the dynamic import, this component is unable to manage it’s SSR lifecycle unless it includes nextjs-specific code.
  • But really all that needs to happen is for pixi to abort loading if the window is not present. (Something like wrapping everything in a giant if (typeof window === 'undefined') 😃 )

Expected Behavior

  • pixi errors gracefully when the window object is not present

Current Behavior

  • pixi cannot run in SSR

Possible Solution

  • Something like wrapping everything in a giant if (typeof window === 'undefined') 😃

Steps to Reproduce

Environment

  • pixi.js - v5.3.3

  • pixi.js version: e.g. 4.7.1

  • Browser & Version: e.g. Chrome 67

  • OS & Version: e.g. Ubuntu 18.04

  • Running Example: url

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:5
  • Comments:16 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
bigtimebuddycommented, May 3, 2022

Released v6.3.1, wait a few minutes for it to publish

3reactions
daflyinbedcommented, Nov 24, 2020

wrap pixi.js in a component

// pixi.tsx
import { useEffect, useRef } from "react";
import { Application, utils } from "pixi.js";
export default function Pixi() {
  const canvasRef = useRef<HTMLCanvasElement>(null);
  useEffect(() => {
    if (process.browser) {
      let type = "WebGL";
      if (!utils.isWebGLSupported()) {
        type = "canvas";
      }
      utils.sayHello(type);
    }
  }, []);
  return <canvas ref={canvasRef} />;
}

and them dynamic import that with no ssr

// pages/PixiPage.tsx
import React from "react";
import dynamic from "next/dynamic";
const Pixi = dynamic(() => import("./pixi"), {
  ssr: false,
}); 
export default function PixiPage() {
  return (
      <Pixi/>
  );
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to solve Next.js window is not defined
ReferenceError: window is not defined is a pretty common error you may run into when using Next.js for the first time but don't...
Read more >
Window is not defined in Next.js React app - Stack Overflow
Next.js is universal, which means it executes code first server-side, then client-side. The window object is only present client-side, so ...
Read more >
How to Fix "window is not defined" in Next.js - Upmostly
This issue has to do with Server-Side Rendering in Next.js. Next.js will by default try to use SSR for your site. This means...
Read more >
How to fix window is not defined error in Next.js
As you can see the error occurs in the terminal itself. That means it is a compilation error. Next.js is compiled/built on Node.js...
Read more >
Fix for Next.js ReferenceError window is not defined
The window object is the reference to an open window in a browser. Therefore, the window object is only available on the client-side....
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