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

Can't create a production build with package imported - Error occurred prerendering page; ReferenceError: window is not defined #112

Open
RamazaniMwemedi opened this issue Oct 8, 2024 · 7 comments

Comments

@RamazaniMwemedi
Copy link

Whenever I try to make a production build with this component imported, it keep throwing an error as shown below

image

I have troubleshooted and I have noticed that the error comes from here
image

I have tried to import it with Dynamics but still no changes,

image

@JayWebtech
Copy link

Same here. Were you able to find a solution?

@Azania-Mokhampane
Copy link

getting the same issue too

@Azania-Mokhampane
Copy link

@JayWebtech @RamazaniMwemedi this worked for me,

const PayWithPaystack  = () => {
     
  const isBrowser = typeof window !== 'undefined';

  if (!isBrowser) {
    return null;
  }

  // Import your Paystack-related code here
  const { usePaystackPayment } = require('react-paystack')

const config = {...}

const initializePayment = usePaystackPayment(config);

 return (
    <button onClick={initializePayment}>
      Pay
    </button>
  );
};

export default PayWithPaystack;

@stephengade
Copy link

Started getting this error immediately I migrate to Nextjs 15, React 19 (Server component)

@RamazaniMwemedi
Copy link
Author

An email from the developer team on how I should go around it.

Here is the link https://paystack.com/docs/api/transaction/#initialize

image

@stephengade
Copy link

Hmmm.... That may be straightforward approach. I should have adopted using the API directly rather than SDK.

But currently I fixed the window error by converting the whole page to client with 'use client'

I first of all mark the component with 'use client'

then in /app/payment/page.tsx, I imported the payment component the dynamically disabling the SSR (of course, this will only work by marking the entire page with 'use client' as well)

on doing that, the error is gone...

@chizidotdev
Copy link

then in /app/payment/page.tsx, I imported the payment component the dynamically disabling the SSR (of course, this will only work by marking the entire page with 'use client' as well)

@stephengade 's comment did the trick

My implementation looks like this, using the next/dynamic utility to import the PaystackButton

- import { PaystackButton } from "react-paystack";

+ import dynamic from 'next/dynamic';
+ const PaystackButton = dynamic(() => import("react-paystack").then((c) => c.PaystackButton), {
+   ssr: false,
+ });

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

5 participants