react-custom-roulette icon indicating copy to clipboard operation
react-custom-roulette copied to clipboard

Error building next js app

Open spandres opened this issue 2 years ago • 6 comments

Problem

I get the following error message after running npm run build:

ReferenceError: window is not defined

Description

I'm importing from a client component ("use client") the wheel component. Unfortunately, the build fails. I verified the problem was the wheel component because after removing it, the build was completed as expected.

I tried the wheel component using the basic setup in the README.

The wheel component works when I run the npm run dev command. The problem is observed only when trying to build the app.

Screenshot 2023-08-02 at 20 21 26

spandres avatar Aug 03 '23 01:08 spandres

Same issue

Gabriel-Passoss avatar Aug 06 '23 18:08 Gabriel-Passoss

My workaround was to create a wrapper component at the layout level.

export default function Wrapper({ children }: {children?: ReactNode}) {
  const [ready, setReady] = useState(false);

  useEffect(() => {
    setReady(true);
  }, []);

  if (!ready) return null;

  return <main>{children}</main>;
}

Just put the wheel component anywhere inside the wrapper, and it should work.

spandres avatar Aug 07 '23 13:08 spandres

My workaround was to create a wrapper component at the layout level.

export default function Wrapper({ children }: {children?: ReactNode}) {
  const [ready, setReady] = useState(false);

  useEffect(() => {
    setReady(true);
  }, []);

  if (!ready) return null;

  return <main>{children}</main>;
}

Just put the wheel component anywhere inside the wrapper, and it should work.

did not worked for me sadly on nextjs

supramaxis avatar Oct 15 '23 06:10 supramaxis

This one worked for me https://www.npmjs.com/package/react-wheel-of-prizes

berthelol avatar Dec 12 '23 06:12 berthelol

Any update on this issue?

abdulsami822 avatar Mar 15 '24 04:03 abdulsami822

worked for me:

Modify your import statement for the Wheel component to use Next.js is dynamic imports with ssr set to false. This tells Next.js to only render the component on the client side.

Old: import { Wheel } from 'react-custom-roulette';

Replace:

import dynamic from 'next/dynamic'; const Wheel = dynamic(() => import('react-custom-roulette').then((mod) => mod.Wheel), { ssr: false, });

const WheelOfLuckyGame = (props) => { ... . return ( <Wheel ...../> )}

buivangiang-12148701 avatar Apr 05 '24 01:04 buivangiang-12148701