🧩서버에서 브라우저 기능을 씀 (window is not defined)
ReferenceError: window is not defined (또는 document / localStorage is not defined)
브라우저에만 있는 window·document·localStorage를 서버에서 실행하려 해서 생기는 에러예요.
🤔 왜 생기나요?
- Next.js 같은 프레임워크는 페이지를 서버에서 먼저 그려요. 그때 브라우저 기능을 썼어요
- 브라우저 전용 라이브러리를 맨 위에서 바로 불러왔어요
🔧 이렇게 해 보세요
- 브라우저 기능은 useEffect 안이나 이벤트 안에서만 써요
- 브라우저 전용 라이브러리는 브라우저에서만 불러오게 해요(동적 import, ssr 끄기)
🤖 AI에게 이렇게 물어보세요
아래 'window is not defined' 에러가 나. 브라우저 기능을 쓰는 곳을 찾아 브라우저에서만 실행되게 고쳐 줘. [여기에 에러 전체를 붙여 넣기]
[ ] 부분은 내 상황에 맞게 바꾸고, 에러는 앞뒤 몇 줄까지 넉넉히 복사해 붙여 넣으세요.
관련 용어
같은 분류의 다른 에러
- 비어 있는 값을 읽으려 함 (undefined)
Cannot read properties of undefined - 함수가 아닌 것을 실행하려 함
is not a function - 정의되지 않은 이름
ReferenceError: … is not defined - JSON이 아닌 응답 (Unexpected token)
Unexpected token '<' … is not valid JSON - 서버 화면과 브라우저 화면이 다름 (Hydration)
Hydration failed - 무한 반복 그리기 (Too many re-renders)
Too many re-renders - 목록에 key가 없음
Each child in a list should have a unique "key" prop - use client가 필요함 (Next.js)
Add the "use client" directive - 함수가 끝없이 자기 자신을 부름 (호출 스택 초과)
RangeError: Maximum call stack size exceeded - 그리는 도중에 다른 컴포넌트를 바꾸려 함
Cannot update a component while rendering a different component - 객체를 화면에 그대로 출력하려 함
Objects are not valid as a React child - 클라이언트 컴포넌트를 async로 만듦
async/await is not yet supported in Client Components - 외부 이미지 주소가 허용되지 않음 (next/image)
Invalid src prop … hostname is not configured under images - Suspense로 감싸야 함 (useSearchParams)
useSearchParams() should be wrapped in a suspense boundary - 처리하지 않은 비동기 에러
Uncaught (in promise) - 날짜를 읽지 못함 (Invalid Date)
Invalid Date - 숫자 자리에 NaN이 나옴
NaN