TypeError: Document is undefined

Document is undefined hiba: böngészői DOM objektumok nem érhetők el szerveroldalon, használj környezet-ellenőrzést.

Document is undefined JavaScript

Hibaüzenet

TypeError: Document is undefined

Hibás kód

console.log(document.title);

Javított kód

if (typeof document !== 'undefined') {
  console.log(document.title);
}

Magyarázat

Document is undefined hiba akkor fordul elő, amikor kliensoldali DOM API-t próbálsz használni szerveroldali környezetben (pl. Next.js SSR). A document csak böngészőben érhető el. A megoldás, hogy ellenőrzöd a környezetet: if (typeof document !== 'undefined') vagy futtatáskor dinamikusan importálod a DOM-specifikus kódot.

További információ: Next.js SSR