오류모음

[Error Handling] window is not defined nextjs

테오구 2022. 3. 10. 20:50
728x90

어떤 에러인가요?

  • 에러 메시지
$ window is not defined nextjs

에러 핸들링 방법

  • Next.js는 universal 하다.

즉, server-side에서 먼저 실행 되고, 그 후에 client-side에서 실행된다. window는 client-side에만 존재한다. 고로 React Component내에서 window를 사용하고 싶다면 componentDidMount내에 코드를 작성해야한다.

if (typeof window !== 'undefined') {
  require('the-lib');
}

에러 핸들링을 위해 참고한 레퍼런스 링크

  • Memory-It 의 Error Handling 예시입니다.
    링크
 

Next.js "window,document is not defined" 해결하는 법

Next.js 개발환경에서 window object를 사용할 때, document is undefined, Cannot read innerWidth of undefined 등의 에러메세지를 마주할 때가 있다. Next.js…

sumini.dev

 

github: https://github.com/codestates/BanThing/issues/201

728x90