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.

HTMLElement is not defined (using Web Components in Next.js)

See original GitHub issue

issue

Describe the issue

when using web components inside a next.js app, you may get this error:

ReferenceError: HTMLElement is not defined

this is just Next.js trying to SSR your web component.

Fix

Instead of

import "wc-spinners/dist/half-circle-spinner.js"

do

function Component() {

  React.useEffect(() => {
    import("wc-spinners/dist/half-circle-spinner.js");
  });
  // etc
	return <div>
		// etc
		<half-circle-spinner />
		</div>
}
}

just filing this for future people who searched like me and found no WC stuff in this repo

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
joamarticocommented, Feb 22, 2021

when I try to import it using useEffect I get this syntax error: “‘import’ and ‘export’ may only appear at the top level” How can I solve It?

0reactions
balazsorban44commented, Jan 27, 2022

This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use Web Components with Next.js and TypeScript
Next.js and Web Components. The next issue you run into is server side rendering. ReferenceError: HTMLElement is not defined.
Read more >
How to use Web Components with Next.js and ... - Swyx
Next.js and Web Components. The next issue you run into is server side rendering. ReferenceError: HTMLElement is not defined.
Read more >
Next Js with react-xarrows gives error "ReferenceError
A great project I found was react-xarrows, however when I am trying to use it in my code, I see an error: ReferenceError:...
Read more >
Solve “document is not defined” errors in Next.js | by WebTutPro
You can import your Scroll component using dynamic imports and the srr: false option. This way your component won't even be rendered on...
Read more >
Web Components in Server-Side Rendered (SSR) and Static ...
Since all JSX elements come with a dangerouslySetInnerHTML property, this approach allows Next.js to not have to run the component.ts file to ...
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