GatsbyJS Build – window is not defined / module is not defined










4















When trying to build my GatsbyJS project I ran into, what seems to be a fairly common issue:



WebpackError: ReferenceError: window is not defined




When googling I found out that this is a fairly common issue. So I moved into componentDidMount with requiring the module like this



if (typeof window !== `undefined`) 
const Flickity = require('react-flickity-component');





Which causes other issues



121:13 warning 'Flickity' is assigned a value but never used no-unused-vars
170:12 error 'Flickity' is not defined react/jsx-no-undef




Moving to the other options available, using webpack to exclude offending modules



exports.onCreateWebpackConfig = ( stage, loaders, actions ) => 
if (stage === "build-html")
actions.setWebpackConfig(
module:
rules: [

test: /react-flickity-component/,
use: loaders.null(),
,

test: /flickity-fullscreen/,
use: loaders.null(),
,
],
,
)






Which also gives me headache



Error: Invariant Violation: Minified React error #130; visit https://reactjs.o rg/docs/error-decoder.html?invariant=130&args=object&args= for the full me ssage or use the non-minified dev environment for full errors and additional h elpful warnings.

- Error

- react-dom-server.node.production.min.js:10 ba
[www]/[react-dom]/cjs/react-dom-server.node.production.min.js :10:312

- react-dom-server.node.production.min.js:11 u
[www]/[react-dom]/cjs/react-dom-server.node.production.min.js :11:166

- react-dom-server.node.production.min.js:39 a.render
[www]/[react-dom]/cjs/react-dom-server.node.production.min.js :39:88

- react-dom-server.node.production.min.js:35 a.read
[www]/[react-dom]/cjs/react-dom-server.node.production.min.js :35:450

- react-dom-server.node.production.min.js:47 renderToString
[www]/[react-dom]/cjs/react-dom-server.node.production.min.js :47:59

- render-page.js:825 Object../.cache/static-entry.js.__webpack_exports__.defau lt
/Users/per.sturesson/WWW/intermission.studio/www/public/rende r-page.js:825:18

- worker.js:35 e
[www]/[gatsby]/dist/utils/worker.js:35:36

- debuggability.js:313 Promise._execute
[www]/[bluebird]/js/release/debuggability.js:313:9

- promise.js:483 Promise._resolveFromExecutor
[www]/[bluebird]/js/release/promise.js:483:18

- promise.js:79 new Promise
[www]/[bluebird]/js/release/promise.js:79:10

- worker.js:31 Promise.map.path
[www]/[gatsby]/dist/utils/worker.js:31:37

- util.js:16 tryCatcher
[www]/[bluebird]/js/release/util.js:16:23

- map.js:61 MappingPromiseArray._promiseFulfilled
[www]/[bluebird]/js/release/map.js:61:38

- promise_array.js:114 MappingPromiseArray.PromiseArray._iterate
[www]/[bluebird]/js/release/promise_array.js:114:31

- promise_array.js:78 MappingPromiseArray.init
[www]/[bluebird]/js/release/promise_array.js:78:10


Any way to get around all this?
If I exclude the module from the project all together it will build successfully.










share|improve this question


























    4















    When trying to build my GatsbyJS project I ran into, what seems to be a fairly common issue:



    WebpackError: ReferenceError: window is not defined




    When googling I found out that this is a fairly common issue. So I moved into componentDidMount with requiring the module like this



    if (typeof window !== `undefined`) 
    const Flickity = require('react-flickity-component');





    Which causes other issues



    121:13 warning 'Flickity' is assigned a value but never used no-unused-vars
    170:12 error 'Flickity' is not defined react/jsx-no-undef




    Moving to the other options available, using webpack to exclude offending modules



    exports.onCreateWebpackConfig = ( stage, loaders, actions ) => 
    if (stage === "build-html")
    actions.setWebpackConfig(
    module:
    rules: [

    test: /react-flickity-component/,
    use: loaders.null(),
    ,

    test: /flickity-fullscreen/,
    use: loaders.null(),
    ,
    ],
    ,
    )






    Which also gives me headache



    Error: Invariant Violation: Minified React error #130; visit https://reactjs.o rg/docs/error-decoder.html?invariant=130&args=object&args= for the full me ssage or use the non-minified dev environment for full errors and additional h elpful warnings.

    - Error

    - react-dom-server.node.production.min.js:10 ba
    [www]/[react-dom]/cjs/react-dom-server.node.production.min.js :10:312

    - react-dom-server.node.production.min.js:11 u
    [www]/[react-dom]/cjs/react-dom-server.node.production.min.js :11:166

    - react-dom-server.node.production.min.js:39 a.render
    [www]/[react-dom]/cjs/react-dom-server.node.production.min.js :39:88

    - react-dom-server.node.production.min.js:35 a.read
    [www]/[react-dom]/cjs/react-dom-server.node.production.min.js :35:450

    - react-dom-server.node.production.min.js:47 renderToString
    [www]/[react-dom]/cjs/react-dom-server.node.production.min.js :47:59

    - render-page.js:825 Object../.cache/static-entry.js.__webpack_exports__.defau lt
    /Users/per.sturesson/WWW/intermission.studio/www/public/rende r-page.js:825:18

    - worker.js:35 e
    [www]/[gatsby]/dist/utils/worker.js:35:36

    - debuggability.js:313 Promise._execute
    [www]/[bluebird]/js/release/debuggability.js:313:9

    - promise.js:483 Promise._resolveFromExecutor
    [www]/[bluebird]/js/release/promise.js:483:18

    - promise.js:79 new Promise
    [www]/[bluebird]/js/release/promise.js:79:10

    - worker.js:31 Promise.map.path
    [www]/[gatsby]/dist/utils/worker.js:31:37

    - util.js:16 tryCatcher
    [www]/[bluebird]/js/release/util.js:16:23

    - map.js:61 MappingPromiseArray._promiseFulfilled
    [www]/[bluebird]/js/release/map.js:61:38

    - promise_array.js:114 MappingPromiseArray.PromiseArray._iterate
    [www]/[bluebird]/js/release/promise_array.js:114:31

    - promise_array.js:78 MappingPromiseArray.init
    [www]/[bluebird]/js/release/promise_array.js:78:10


    Any way to get around all this?
    If I exclude the module from the project all together it will build successfully.










    share|improve this question
























      4












      4








      4








      When trying to build my GatsbyJS project I ran into, what seems to be a fairly common issue:



      WebpackError: ReferenceError: window is not defined




      When googling I found out that this is a fairly common issue. So I moved into componentDidMount with requiring the module like this



      if (typeof window !== `undefined`) 
      const Flickity = require('react-flickity-component');





      Which causes other issues



      121:13 warning 'Flickity' is assigned a value but never used no-unused-vars
      170:12 error 'Flickity' is not defined react/jsx-no-undef




      Moving to the other options available, using webpack to exclude offending modules



      exports.onCreateWebpackConfig = ( stage, loaders, actions ) => 
      if (stage === "build-html")
      actions.setWebpackConfig(
      module:
      rules: [

      test: /react-flickity-component/,
      use: loaders.null(),
      ,

      test: /flickity-fullscreen/,
      use: loaders.null(),
      ,
      ],
      ,
      )






      Which also gives me headache



      Error: Invariant Violation: Minified React error #130; visit https://reactjs.o rg/docs/error-decoder.html?invariant=130&args=object&args= for the full me ssage or use the non-minified dev environment for full errors and additional h elpful warnings.

      - Error

      - react-dom-server.node.production.min.js:10 ba
      [www]/[react-dom]/cjs/react-dom-server.node.production.min.js :10:312

      - react-dom-server.node.production.min.js:11 u
      [www]/[react-dom]/cjs/react-dom-server.node.production.min.js :11:166

      - react-dom-server.node.production.min.js:39 a.render
      [www]/[react-dom]/cjs/react-dom-server.node.production.min.js :39:88

      - react-dom-server.node.production.min.js:35 a.read
      [www]/[react-dom]/cjs/react-dom-server.node.production.min.js :35:450

      - react-dom-server.node.production.min.js:47 renderToString
      [www]/[react-dom]/cjs/react-dom-server.node.production.min.js :47:59

      - render-page.js:825 Object../.cache/static-entry.js.__webpack_exports__.defau lt
      /Users/per.sturesson/WWW/intermission.studio/www/public/rende r-page.js:825:18

      - worker.js:35 e
      [www]/[gatsby]/dist/utils/worker.js:35:36

      - debuggability.js:313 Promise._execute
      [www]/[bluebird]/js/release/debuggability.js:313:9

      - promise.js:483 Promise._resolveFromExecutor
      [www]/[bluebird]/js/release/promise.js:483:18

      - promise.js:79 new Promise
      [www]/[bluebird]/js/release/promise.js:79:10

      - worker.js:31 Promise.map.path
      [www]/[gatsby]/dist/utils/worker.js:31:37

      - util.js:16 tryCatcher
      [www]/[bluebird]/js/release/util.js:16:23

      - map.js:61 MappingPromiseArray._promiseFulfilled
      [www]/[bluebird]/js/release/map.js:61:38

      - promise_array.js:114 MappingPromiseArray.PromiseArray._iterate
      [www]/[bluebird]/js/release/promise_array.js:114:31

      - promise_array.js:78 MappingPromiseArray.init
      [www]/[bluebird]/js/release/promise_array.js:78:10


      Any way to get around all this?
      If I exclude the module from the project all together it will build successfully.










      share|improve this question














      When trying to build my GatsbyJS project I ran into, what seems to be a fairly common issue:



      WebpackError: ReferenceError: window is not defined




      When googling I found out that this is a fairly common issue. So I moved into componentDidMount with requiring the module like this



      if (typeof window !== `undefined`) 
      const Flickity = require('react-flickity-component');





      Which causes other issues



      121:13 warning 'Flickity' is assigned a value but never used no-unused-vars
      170:12 error 'Flickity' is not defined react/jsx-no-undef




      Moving to the other options available, using webpack to exclude offending modules



      exports.onCreateWebpackConfig = ( stage, loaders, actions ) => 
      if (stage === "build-html")
      actions.setWebpackConfig(
      module:
      rules: [

      test: /react-flickity-component/,
      use: loaders.null(),
      ,

      test: /flickity-fullscreen/,
      use: loaders.null(),
      ,
      ],
      ,
      )






      Which also gives me headache



      Error: Invariant Violation: Minified React error #130; visit https://reactjs.o rg/docs/error-decoder.html?invariant=130&args=object&args= for the full me ssage or use the non-minified dev environment for full errors and additional h elpful warnings.

      - Error

      - react-dom-server.node.production.min.js:10 ba
      [www]/[react-dom]/cjs/react-dom-server.node.production.min.js :10:312

      - react-dom-server.node.production.min.js:11 u
      [www]/[react-dom]/cjs/react-dom-server.node.production.min.js :11:166

      - react-dom-server.node.production.min.js:39 a.render
      [www]/[react-dom]/cjs/react-dom-server.node.production.min.js :39:88

      - react-dom-server.node.production.min.js:35 a.read
      [www]/[react-dom]/cjs/react-dom-server.node.production.min.js :35:450

      - react-dom-server.node.production.min.js:47 renderToString
      [www]/[react-dom]/cjs/react-dom-server.node.production.min.js :47:59

      - render-page.js:825 Object../.cache/static-entry.js.__webpack_exports__.defau lt
      /Users/per.sturesson/WWW/intermission.studio/www/public/rende r-page.js:825:18

      - worker.js:35 e
      [www]/[gatsby]/dist/utils/worker.js:35:36

      - debuggability.js:313 Promise._execute
      [www]/[bluebird]/js/release/debuggability.js:313:9

      - promise.js:483 Promise._resolveFromExecutor
      [www]/[bluebird]/js/release/promise.js:483:18

      - promise.js:79 new Promise
      [www]/[bluebird]/js/release/promise.js:79:10

      - worker.js:31 Promise.map.path
      [www]/[gatsby]/dist/utils/worker.js:31:37

      - util.js:16 tryCatcher
      [www]/[bluebird]/js/release/util.js:16:23

      - map.js:61 MappingPromiseArray._promiseFulfilled
      [www]/[bluebird]/js/release/map.js:61:38

      - promise_array.js:114 MappingPromiseArray.PromiseArray._iterate
      [www]/[bluebird]/js/release/promise_array.js:114:31

      - promise_array.js:78 MappingPromiseArray.init
      [www]/[bluebird]/js/release/promise_array.js:78:10


      Any way to get around all this?
      If I exclude the module from the project all together it will build successfully.







      reactjs gatsby






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 15 '18 at 15:31









      INTINT

      30921136




      30921136






















          1 Answer
          1






          active

          oldest

          votes


















          0














          I successfully got flickity to build in my gatsby project by wrapping my flickity code in a window check.



          I'm not sure if this is 'correct' or 'proper' but it does build:



          import React, Component from 'react'
          import Flickity from 'react-flickity-component'

          import './flixity.css'

          const flickityOptions =
          wrapAround: true,
          adaptiveHeight: true,
          dragThreshold: 10


          export default class Carousel extends Component
          render()
          if (typeof window !== 'undefined')
          return (
          <div class="container py-3 mb-5">
          <Flickity options=flickityOptions>
          <img src="https://via.placeholder.com/800x400?text=Hero1" />
          <img src="https://via.placeholder.com/800x400?text=Hero2" />
          <img src="https://via.placeholder.com/800x400?text=Hero3" />
          <img src="https://via.placeholder.com/800x400?text=Hero4" />
          <img src="https://via.placeholder.com/800x400?text=Hero5" />
          <img src="https://via.placeholder.com/800x400?text=Hero6" />
          </Flickity>
          </div>
          )

          return null







          share|improve this answer























          • Think I decided to move on to another slider to get around this issue. But will definitely check it out in the future. Thanks!

            – INT
            Mar 9 at 19:56










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53322761%2fgatsbyjs-build-window-is-not-defined-module-is-not-defined%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          I successfully got flickity to build in my gatsby project by wrapping my flickity code in a window check.



          I'm not sure if this is 'correct' or 'proper' but it does build:



          import React, Component from 'react'
          import Flickity from 'react-flickity-component'

          import './flixity.css'

          const flickityOptions =
          wrapAround: true,
          adaptiveHeight: true,
          dragThreshold: 10


          export default class Carousel extends Component
          render()
          if (typeof window !== 'undefined')
          return (
          <div class="container py-3 mb-5">
          <Flickity options=flickityOptions>
          <img src="https://via.placeholder.com/800x400?text=Hero1" />
          <img src="https://via.placeholder.com/800x400?text=Hero2" />
          <img src="https://via.placeholder.com/800x400?text=Hero3" />
          <img src="https://via.placeholder.com/800x400?text=Hero4" />
          <img src="https://via.placeholder.com/800x400?text=Hero5" />
          <img src="https://via.placeholder.com/800x400?text=Hero6" />
          </Flickity>
          </div>
          )

          return null







          share|improve this answer























          • Think I decided to move on to another slider to get around this issue. But will definitely check it out in the future. Thanks!

            – INT
            Mar 9 at 19:56















          0














          I successfully got flickity to build in my gatsby project by wrapping my flickity code in a window check.



          I'm not sure if this is 'correct' or 'proper' but it does build:



          import React, Component from 'react'
          import Flickity from 'react-flickity-component'

          import './flixity.css'

          const flickityOptions =
          wrapAround: true,
          adaptiveHeight: true,
          dragThreshold: 10


          export default class Carousel extends Component
          render()
          if (typeof window !== 'undefined')
          return (
          <div class="container py-3 mb-5">
          <Flickity options=flickityOptions>
          <img src="https://via.placeholder.com/800x400?text=Hero1" />
          <img src="https://via.placeholder.com/800x400?text=Hero2" />
          <img src="https://via.placeholder.com/800x400?text=Hero3" />
          <img src="https://via.placeholder.com/800x400?text=Hero4" />
          <img src="https://via.placeholder.com/800x400?text=Hero5" />
          <img src="https://via.placeholder.com/800x400?text=Hero6" />
          </Flickity>
          </div>
          )

          return null







          share|improve this answer























          • Think I decided to move on to another slider to get around this issue. But will definitely check it out in the future. Thanks!

            – INT
            Mar 9 at 19:56













          0












          0








          0







          I successfully got flickity to build in my gatsby project by wrapping my flickity code in a window check.



          I'm not sure if this is 'correct' or 'proper' but it does build:



          import React, Component from 'react'
          import Flickity from 'react-flickity-component'

          import './flixity.css'

          const flickityOptions =
          wrapAround: true,
          adaptiveHeight: true,
          dragThreshold: 10


          export default class Carousel extends Component
          render()
          if (typeof window !== 'undefined')
          return (
          <div class="container py-3 mb-5">
          <Flickity options=flickityOptions>
          <img src="https://via.placeholder.com/800x400?text=Hero1" />
          <img src="https://via.placeholder.com/800x400?text=Hero2" />
          <img src="https://via.placeholder.com/800x400?text=Hero3" />
          <img src="https://via.placeholder.com/800x400?text=Hero4" />
          <img src="https://via.placeholder.com/800x400?text=Hero5" />
          <img src="https://via.placeholder.com/800x400?text=Hero6" />
          </Flickity>
          </div>
          )

          return null







          share|improve this answer













          I successfully got flickity to build in my gatsby project by wrapping my flickity code in a window check.



          I'm not sure if this is 'correct' or 'proper' but it does build:



          import React, Component from 'react'
          import Flickity from 'react-flickity-component'

          import './flixity.css'

          const flickityOptions =
          wrapAround: true,
          adaptiveHeight: true,
          dragThreshold: 10


          export default class Carousel extends Component
          render()
          if (typeof window !== 'undefined')
          return (
          <div class="container py-3 mb-5">
          <Flickity options=flickityOptions>
          <img src="https://via.placeholder.com/800x400?text=Hero1" />
          <img src="https://via.placeholder.com/800x400?text=Hero2" />
          <img src="https://via.placeholder.com/800x400?text=Hero3" />
          <img src="https://via.placeholder.com/800x400?text=Hero4" />
          <img src="https://via.placeholder.com/800x400?text=Hero5" />
          <img src="https://via.placeholder.com/800x400?text=Hero6" />
          </Flickity>
          </div>
          )

          return null








          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Feb 28 at 21:14









          rickymetzrickymetz

          135




          135












          • Think I decided to move on to another slider to get around this issue. But will definitely check it out in the future. Thanks!

            – INT
            Mar 9 at 19:56

















          • Think I decided to move on to another slider to get around this issue. But will definitely check it out in the future. Thanks!

            – INT
            Mar 9 at 19:56
















          Think I decided to move on to another slider to get around this issue. But will definitely check it out in the future. Thanks!

          – INT
          Mar 9 at 19:56





          Think I decided to move on to another slider to get around this issue. But will definitely check it out in the future. Thanks!

          – INT
          Mar 9 at 19:56



















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53322761%2fgatsbyjs-build-window-is-not-defined-module-is-not-defined%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          Barbados

          How to read a connectionString WITH PROVIDER in .NET Core?

          Node.js Script on GitHub Pages or Amazon S3