Gatsby: WebpackError : ReferenceError : ๋ฌธ์„œ๊ฐ€ ์ •์˜๋˜์ง€ ์•Š์Œ-IE ํ™•์ธ

์— ๋งŒ๋“  2019๋…„ 06์›” 18์ผ  ยท  1๋…ผํ‰  ยท  ์ถœ์ฒ˜: gatsbyjs/gatsby

๊ธฐ์ˆ 

์ตœ๊ทผ์— ์‚ฌ์šฉ์ž๊ฐ€ Internet Explorer๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด const isIE = /*@cc_on!@*/ false || !!document.documentMode ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ  ์กฐ๊ฑด๋ถ€๋กœ ์ตœ์‹  ๋ธŒ๋ผ์šฐ์ € (Chrome / Firefox)๋ฅผ ์‚ฌ์šฉํ•˜๋„๋ก ์š”์ฒญํ•˜๋Š” ๋ฉ”์‹œ์ง€๋ฅผ ์ถ”๊ฐ€ํ–ˆ์Šต๋‹ˆ๋‹ค. gatsby develop ์‚ฌ์šฉํ•˜๋ฉด ์˜ˆ์ƒ๋Œ€๋กœ ์ž‘๋™ํ•˜์ง€๋งŒ gatsby build ์‚ฌ์šฉํ•˜๋ฉด ์‹คํŒจํ•ฉ๋‹ˆ๋‹ค.

error Building static HTML failed
WebpackError: ReferenceError: document is not defined

์žฌํ˜„ ๋‹จ๊ณ„

const isIE = /*@cc_on!@*/ false || !!document.documentMode ์„ ์–ธํ•˜๊ณ  js ํŒŒ์ผ ์ค‘ ํ•˜๋‚˜์—์„œ const {isIE && ()} ์— ๋Œ€ํ•œ ํ™•์ธ์„ ์‹คํ–‰ ํ•œ ํ›„ ํ”„๋กœ์ ํŠธ ํด๋” ๋‚ด์—์„œ gatsby build ์‹คํ–‰. Gatsby develop ์˜ˆ์ƒ๋Œ€๋กœ ์ž˜ ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค.

์˜ˆ์ƒ ๊ฒฐ๊ณผ

์‚ฌ์ดํŠธ๊ฐ€ ์˜ค๋ฅ˜์—†์ด ๋นŒ๋“œ๋˜๊ณ  ์‚ฌ์šฉ์ž๊ฐ€ IE๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ ์กฐ๊ฑด๋ถ€๋กœ ๋ฉ”์‹œ์ง€๋ฅผ ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค.

์‹ค์ œ ๊ฒฐ๊ณผ

์‚ฌ์ดํŠธ๋Š” gatsby build ๋กœ ๋นŒ๋“œ ํ•  ์ˆ˜ ์—†์ง€๋งŒ gatsby develop ๊ดœ์ฐฎ์Šต๋‹ˆ๋‹ค.

WebpackError: ReferenceError: document is not defined
  - header.js:20 Module../src/components/header.js
    lib/src/components/header.js:20:27
  - bootstrap:19 __webpack_require__
    lib/webpack/bootstrap:19:1
  - bootstrap:19 __webpack_require__
    lib/webpack/bootstrap:19:1
  - bootstrap:19 __webpack_require__
    lib/webpack/bootstrap:19:1
  - sync-requires.js:8 Object../.cache/sync-requires.js
    lib/.cache/sync-requires.js:8:58
  - bootstrap:19 __webpack_require__
    lib/webpack/bootstrap:19:1
  - static-entry.js:9 Module../.cache/static-entry.js
    lib/.cache/static-entry.js:9:22
  - bootstrap:19 __webpack_require__
    lib/webpack/bootstrap:19:1
  - bootstrap:83 
    lib/webpack/bootstrap:83:1
  - universalModuleDefinition:3 webpackUniversalModuleDefinition
    lib/webpack/universalModuleDefinition:3:1
  - universalModuleDefinition:10 Object.<anonymous>
    lib/webpack/universalModuleDefinition:10:2
  - static-entry.js:84 Module._compile
    lib/.cache/static-entry.js:84:3
  - static-entry.js:101 Object.Module._extensions..js
    lib/.cache/static-entry.js:101:9

ํ™˜๊ฒฝ

  System:
    OS: macOS 10.14.5
    CPU: (8) x64 Intel(R) Core(TM) i5-8259U CPU @ 2.30GHz
    Shell: 3.2.57 - /bin/bash
  Binaries:
    Node: 10.15.3 - /usr/local/bin/node
    Yarn: 1.15.2 - /usr/local/bin/yarn
    npm: 6.9.0 - /usr/local/bin/npm
  Languages:
    Python: 2.7.10 - /usr/bin/python
  Browsers:
    Chrome: 75.0.3770.90
    Safari: 12.1.1
  npmPackages:
    gatsby: ^2.9.0 => 2.9.0 
    gatsby-image: ^2.1.3 => 2.1.3 
    gatsby-plugin-google-analytics: ^2.0.21 => 2.0.21 
    gatsby-plugin-hotjar: ^1.0.1 => 1.0.1 
    gatsby-plugin-manifest: ^2.1.1 => 2.1.1 
    gatsby-plugin-offline: ^2.1.1 => 2.1.1 
    gatsby-plugin-react-helmet: ^3.0.12 => 3.0.12 
    gatsby-plugin-sass: ^2.0.11 => 2.0.11 
    gatsby-plugin-sharp: ^2.1.3 => 2.1.3 
    gatsby-plugin-sitemap: ^2.1.0 => 2.1.0 
    gatsby-remark-external-links: 0.0.4 => 0.0.4 
    gatsby-source-contentful: ^2.0.69 => 2.0.69 
    gatsby-source-filesystem: ^2.0.39 => 2.0.39 
    gatsby-transformer-remark: ^2.3.12 => 2.3.12 
    gatsby-transformer-sharp: ^2.1.21 => 2.1.21 
  npmGlobalPackages:
    gatsby-cli: 2.6.7
question or discussion

๊ฐ€์žฅ ์œ ์šฉํ•œ ๋Œ“๊ธ€

@ j-651 "๋ธŒ๋ผ์šฐ์ €์—์„œ"ํ™•์ธ์„ ์›ํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ์œ„ํ•œ ํ”„๋ก์‹œ๋กœ ์ˆ˜๋ช…์ฃผ๊ธฐ ์ด๋ฒคํŠธ ( componentDidMount ) ๋˜๋Š” ํ›„ํฌ ( useEffect )๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ฝ”๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

import React, { useState, useEffect } from 'react'

function Something() {
  const [isIE, setIsIE] = useState(false)
  useEffect(() => {
    setIsIE(/*@cc_on!@*/ false || !!document.documentMode)
  }, [])

  return (
    <p>{isIE ? 'Internet Explorer' : 'Not IE'}</p>
  )
}

export default Something

๋‹ต๋ณ€์œผ๋กœ ์ข…๋ฃŒํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์งˆ๋ฌธ ํ•ด ์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค!

>๋ชจ๋“  ๋Œ“๊ธ€

@ j-651 "๋ธŒ๋ผ์šฐ์ €์—์„œ"ํ™•์ธ์„ ์›ํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ์œ„ํ•œ ํ”„๋ก์‹œ๋กœ ์ˆ˜๋ช…์ฃผ๊ธฐ ์ด๋ฒคํŠธ ( componentDidMount ) ๋˜๋Š” ํ›„ํฌ ( useEffect )๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ฝ”๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

import React, { useState, useEffect } from 'react'

function Something() {
  const [isIE, setIsIE] = useState(false)
  useEffect(() => {
    setIsIE(/*@cc_on!@*/ false || !!document.documentMode)
  }, [])

  return (
    <p>{isIE ? 'Internet Explorer' : 'Not IE'}</p>
  )
}

export default Something

๋‹ต๋ณ€์œผ๋กœ ์ข…๋ฃŒํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์งˆ๋ฌธ ํ•ด ์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค!

์ด ํŽ˜์ด์ง€๊ฐ€ ๋„์›€์ด ๋˜์—ˆ๋‚˜์š”?
0 / 5 - 0 ๋“ฑ๊ธ‰