ReferenceError: document is not defined
💡 에러가 발생한 상황
- 회사에서 Next.js로 개발된 관리자 페이지 배포를 맡아 진행하고 있었는데, AWS S3에 소스코드를 업로드 하기 위해 yarn build 명령어를 실행 시켰는데 위와 같은 오류가 발생하였다.
- 그런데 에러를 읽어보면 document가 정의 되지 않았다는 말과 함께 문제의 원인이 되는 파일은 quill.js 라는 라이브러리 모듈 파일,,? 이었기에 오류를 해석해도 원인이 뭔지 잘모르겠었다.
const QuillBundle =
// 해결 코드 typeof window === "object" ? ... : () => false
typeof window === "object"
? dynamic({
modules: () => {
const components = {
ReactQuill: import("react-quill"),
ImageResize: import("quill-image-resize-module"),
};
return components;
},
ssr: false,
render: (props, { ReactQuill, ImageResize }) => {
const { Quill } = ReactQuill;
Quill.register("modules/imageResize", ImageResize, true);
return (
<ReactQuill
ref={(el) => props.setRef(el)}
modules={props.module}
className={props.className}
defaultValue={props.defaultValue}
onChange={props.onChange}
/>
);
},
})
: () => false;
💡 도움을 받은 github issue 글,,
document is not defined #292