HTMLElement is not defined (using Web Components in Next.js)
See original GitHub issueissue
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:
- Created 3 years ago
- Reactions:5
- Comments:7 (2 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

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?
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.