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.

Facing a ReferenceError: self is not defined while installing on NextJS

See original GitHub issue

Hi there,

Thank you for creating such a beautiful piece of QRCode art! Really thankful for the project!

Sadly, when I had attempted to install and run QR-code-styling, the code seem to be broken.

Primarily, the issue came up when I started to run an import QRCodeStyling from "qr-code-styling"; in my NextJS setup.

  • “next”: “latest”,
  • “qr-code-styling”: “^1.6.0-rc.1”, (Also tried v1.5.0 but also didn’t work)
  • “react”: “^17.0.2”,

Here are the error messages of ReferenceError: self is not defined:


Screenshot 2021-10-31 at 9 53 53 PM Screenshot 2021-10-31 at 9 55 43 PM

Hope you may help assist with the issue. Thank you so much! Cheers!

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:5

github_iconTop GitHub Comments

6reactions
AlexandrePhilibertcommented, Nov 12, 2021

I got around this issue by using ES6 dynamic imports : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import#dynamic_imports

Example:

const ref = useRef<HTMLDivElement>(null);

useEffect(() => {
    // Dynamically import qr-code-styling only client-side
    if (typeof window !== "undefined") {
      import("qr-code-styling").then({ default: QRCodeStyling }) => {
        const qrCode = new QRCodeStyling({
          width: 200,
          height: 200,
          data: "https://google.com",
        });

        qrCode.append(ref.current);
      });
   }
}, []);
5reactions
T04435commented, Jan 30, 2022

I faced this issue as well my solution uses dynamic import from NEXTJS

https://nextjs.org/docs/advanced-features/dynamic-import

So Create a component QR.tsx (whatever really)

and on you Page imported like, important bit is to disable ssr

const DynamicQRCode = dynamic(() => import('@Components/QRCode/QRCode'), {
  ssr: false
});
Read more comments on GitHub >

github_iconTop Results From Across the Web

reactjs - Why am I getting ReferenceError: self is not defined ...
The error occurs because the library requires Web APIs to work, which are not available when Next.js pre-renders the page on the server-side ......
Read more >
middlewares - ReferenceError: self is not defined · Issue #38149
I'm experiencing the same issue. Upgraded to Next.js v12.2 and migrated the middleware accordingly. But builds are breaking with the exact same ...
Read more >
self is not defined nextjs | The AI Search Engine You Control
The error occurs because the library requires Web APIs to work, which are not available when Next.js pre-renders the page on the server-side....
Read more >
self is not defined when I import a client-side library?-Reactjs
The error occurs because the library requires Web APIs to work, which are not available when Next.js pre-renders the page on the server-side....
Read more >
Is there is any possibility to have Web Amity UI Kit working ...
The error we've been facing is always the same for UI Kit from the source code ... 2 ReferenceError: Element is not defined...
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