Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"window not defined" error in NextJS app #47

Closed
SIRHAMY opened this issue May 10, 2020 · 6 comments
Closed

"window not defined" error in NextJS app #47

SIRHAMY opened this issue May 10, 2020 · 6 comments

Comments

@SIRHAMY
Copy link

SIRHAMY commented May 10, 2020

This happens because p5 is imported before window is defined (e.g. server-side rendering and build step). By moving the require("p5") line into componentDidMount this can be resolved.

@jamesrweb
Copy link
Collaborator

jamesrweb commented Sep 23, 2020

Hi @SIRHAMY, thanks for raising the issue. As this is a client side project, the assumption is that window will always be defined.

You could attempt lazy loading your modules - this would be my preferred way of doing things.

Alternatively you could just add a regular script tag to your HTML file and link it to our unpkg link for the script, thus avoiding the build step entirely.

@pikeas
Copy link

pikeas commented Dec 29, 2020

Just bit by this as well. I'm creating the wrapper component in useEffect(..., []), which should only run on the client side when window exists, but I'm still seeing "window not defined" errors.

I'll try lazy loading and a regular script tag if that fails, but in either case I'm worried about missing out on Intellisense and type checking, and confused about why this would happen when scoped to a client side only block.

@halilatilla
Copy link

halilatilla commented Jun 7, 2021

Just bit by this as well. I'm creating the wrapper component in useEffect(..., []), which should only run on the client side when window exists, but I'm still seeing "window not defined" errors.

I'll try lazy loading and a regular script tag if that fails, but in either case I'm worried about missing out on Intellisense and type checking, and confused about why this would happen when scoped to a client side only block.

You can use with Nextjs like that.

import dynamic from 'next/dynamic'

const P5comp = dynamic( () => import('....'), { ssr: false } )

@mak1986
Copy link

mak1986 commented Nov 25, 2021

import { P5Instance, Sketch, P5WrapperProps } from 'react-p5-wrapper'
const ReactP5Wrapper = dynamic(() => import('react-p5-wrapper')
    .then(mod => mod.ReactP5Wrapper as any), {
    ssr: false
}) as unknown as React.NamedExoticComponent<P5WrapperProps>

@Allakazan
Copy link

Having the same problem using Remix. Can someone help me ?

@jamesrweb
Copy link
Collaborator

Create a new issue and provide all code and context such as error messages, for what you're trying to do. Ideally with a minimum viable example I can run on repl.it or stackblitz for example.

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

No branches or pull requests

6 participants