Next.js Discord

Discord Forum

ReferenceError: Element is not defined

Unanswered
Bashamega posted this in #help-forum
Open in Discord
Hello:)
I was creating a next js website, then wen I published it to vercel, it gave me and Element is not defined error, in a fie that I don't have in the .next folder.
The error is in the error.txt.
And when I try npm run dev it works, but gives me the same error when I use npm run build
This is the github repo: https://github.com/Bashamega/webdesignertool

20 Replies

I don't find anything that might cause the error during pre-render despite your editor.js library. Since they might interact with Native API when on init, it can cause problems during pre-rendering

Try removing other code except your imports (including const edjsHTML = require("editorjs-html")) and build again? Also keep your useEffect since it won't cause any errors during pre-render
I can't delete this lib, the full project is based on it
updated code
I think you must make it client only (disable pre-render entirely)
It runs correctly when I make it with run dev
I have only this page on api
In this case, you must do:
const [mount, setMount] = useState(false)

useEffect(() => {
  setMount(true)
}, [])

if (mount) return <Editor />
return <p>Loading editor</p>
and put your editor inside of the component
let me try it and get back to you
I have to go now
Hello
it is not workking
I spent some time and found this:
https://github.com/codex-team/editor.js/issues/1036

I have done some tests myself, it's because editor.js referenced window in its code (which is available on client side only). When you import the package, it throws the error.
Therefore, you have to wrap it into a dynamic import:
const Editor = dynamic(() => import("./Editor"), { ssr: false });

Then Next.js won't load these imports during pre-rendering. import Editor from "./Editor" doesn't work as it still loads the imports.
I don't under stand hoow to edit the code sorry
if you could please make a pr on the github repo
Now put your editor into editor.tsx, and replace the editor on your page with:
"use client"
const Editor = dynamic(() => import("./editor"), { ssr: false });

export default function EditorWrapper() {
  return <Editor />
}
(dynamic function is imported from next/dynamic