Error: window is not defined

2022. 1. 18. 20:28·Typescript
목차
  1. 오류내용
  2. 원인

오류내용

ReferenceError: window is not defined
    at new PaypalButton (C:\Users\gmleh\Desktop\react-shoppingmall\front\node_modules\react-paypal-express-checkout\dist\index.js:90:13)
    at processChild (C:\Users\gmleh\Desktop\react-shoppingmall\front\node_modules\react-dom\cjs\react-dom-server.node.development.js:3305:14)
    at resolve (C:\Users\gmleh\Desktop\react-shoppingmall\front\node_modules\react-dom\cjs\react-dom-server.node.development.js:3270:5)
    at ReactDOMServerRenderer.render (C:\Users\gmleh\Desktop\react-shoppingmall\front\node_modules\react-dom\cjs\react-dom-server.node.development.js:3753:22)
    at ReactDOMServerRenderer.read (C:\Users\gmleh\Desktop\react-shoppingmall\front\node_modules\react-dom\cjs\react-dom-server.node.development.js:3690:29)
    at renderToString (C:\Users\gmleh\Desktop\react-shoppingmall\front\node_modules\react-dom\cjs\react-dom-server.node.development.js:4298:27)
    at renderPage (C:\Users\gmleh\Desktop\react-shoppingmall\front\node_modules\next\dist\next-server\server\render.js:50:851)
    at Object.ctx.renderPage (C:\Users\gmleh\Desktop\react-shoppingmall\front\.next\server\pages\_document.js:862:30)
    at Function.getInitialProps (C:\Users\gmleh\Desktop\react-shoppingmall\front\.next\server\pages\_document.js:299:19)
    at Function.getInitialProps (C:\Users\gmleh\Desktop\react-shoppingmall\front\.next\server\pages\_document.js:870:87)
Error: "MyDocument.getInitialProps()" should resolve to an object. But found "undefined" instead.
    at loadGetInitialProps (C:\Users\gmleh\Desktop\react-shoppingmall\front\node_modules\next\dist\next-server\lib\utils.js:5:295)
    at async renderToHTML (C:\Users\gmleh\Desktop\react-shoppingmall\front\node_modules\next\dist\next-server\server\render.js:50:1107)
    at async C:\Users\gmleh\Desktop\react-shoppingmall\front\node_modules\next\dist\next-server\server\next-server.js:81:66
    at async C:\Users\gmleh\Desktop\react-shoppingmall\front\node_modules\next\dist\next-server\server\next-server.js:74:142
    at async DevServer.renderToHTMLWithComponents (C:\Users\gmleh\Desktop\react-shoppingmall\front\node_modules\next\dist\next-server\server\next-server.js:106:365)
    at async DevServer.renderToHTML (C:\Users\gmleh\Desktop\react-shoppingmall\front\node_modules\next\dist\next-server\server\next-server.js:107:255)
    at async DevServer.renderToHTML (C:\Users\gmleh\Desktop\react-shoppingmall\front\node_modules\next\dist\server\next-dev-server.js:34:578)
    at async DevServer.render (C:\Users\gmleh\Desktop\react-shoppingmall\front\node_modules\next\dist\next-server\server\next-server.js:55:236)
    at async Object.fn (C:\Users\gmleh\Desktop\react-shoppingmall\front\node_modules\next\dist\next-server\server\next-server.js:40:264)
    at async Router.execute (C:\Users\gmleh\Desktop\react-shoppingmall\front\node_modules\next\dist\next-server\server\router.js:23:67)

 

원인

next.js 에서 서버사이드렌더링을 적용할 때 발생하였다.

PayPal 결제모듈에서 이 에러가 발생하였다. 

이는 초기에 next.js에서 웹페이지가 렌더링이 될때는  window가 선언되지 않기 때문에

참조할 수 없어 발생한 문제였다.

 

 

 

해결

Dynamic Import - With no SSR

 

이에 대한 해결책으로 next js 에서 제공하는 Dynamic Import 라는 것을 사용하였다.

Dynamic Import는 정적인 모듈 import를 필요한 시점에 import 할 수 있게 해준다.

즉 필요하지도 않은 모듈을 페이지가 렌더링 할때 import 하는 것이 아니라 

필요할 때 import 하는 것이다. 이는 초기 렌더링시에 성능적으로도 효율적이다.

 

 

import dynamic from 'next/dynamic'
const DynamicPaypalComponent = dynamic(() => import('../components/Paypal'),{ ssr: false })
<DynamicPaypalComponent/>

 

nextjs - Dynamic Import 링크 

https://nextjs.org/docs/advanced-features/dynamic-import

'Typescript' 카테고리의 다른 글

Error : Property 'sagaTask' does not exist on type 'Store<any, AnyAction> & { dispatch: unknown; }'.ts(2339)  (0) 2022.01.30
ERROR : Object is possibly 'undefined'.ts(2532)  (0) 2022.01.26
  1. 오류내용
  2. 원인
'Typescript' 카테고리의 다른 글
  • Error : Property 'sagaTask' does not exist on type 'Store<any, AnyAction> & { dispatch: unknown; }'.ts(2339)
  • ERROR : Object is possibly 'undefined'.ts(2532)
king_hd
king_hd
웹 개발 공부를 위한 블로그입니다.
king_hd
웹 개발 기록
king_hd
전체
오늘
어제
  • 분류 전체보기
    • HTML
    • CSS
    • JS
    • Typescript
    • React
    • nodejs
    • Redux
    • Git
    • 오류모음
    • Webpack
    • 자료구조
    • 네트워크
    • Electron
    • 배포
    • Docker
    • 프로그래머스

블로그 메뉴

  • 홈

링크

공지사항

인기 글

태그

env파일 환경변수 인식
webpack
const
docker
리덕스
docker mysql workbench 연동하는 법
propertykey
리덕스사가
https 인증서 발급
let
Redux-saga
프론트엔드
웹퍼블리셔
Hook
웹개발
프로그래머스
Redux
TypeScript
4차산업혁명동아리
타입스크립트
코딩테스트
JavaScript
ec2에 docker 설치
인증서 자동갱신
next.js
자바스크립트
react
백엔드
TAVE
mysqld: can't open file: 'mysql.ibd' (errno: 0 - )

최근 댓글

최근 글

hELLO· Designed By정상우.v4.6.0
king_hd
Error: window is not defined
상단으로

티스토리툴바

단축키

내 블로그

내 블로그 - 관리자 홈 전환
Q
Q
새 글 쓰기
W
W

블로그 게시글

글 수정 (권한 있는 경우)
E
E
댓글 영역으로 이동
C
C

모든 영역

이 페이지의 URL 복사
S
S
맨 위로 이동
T
T
티스토리 홈 이동
H
H
단축키 안내
Shift + /
⇧ + /

* 단축키는 한글/영문 대소문자로 이용 가능하며, 티스토리 기본 도메인에서만 동작합니다.