question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

WebpackError: ReferenceError: document is not defined

See original GitHub issue

Hi! First of all, thank you for this neat library.

I’m using it with gatsby. I’m instantiating it in useEffect like this

const scroll = new LocomotiveScroll({
      el: scrollRef.current,
      smooth: true,
      smoothMobile: true,
    })

Then I create the ref

const scrollRef = useRef(null)

and assign it to my container

<div ref={scrollRef}></div>

So far so good, it works in development mode in gatsby (so gatsby develop). The problem arises when I go to production, so with gatsby build.

WebpackError: ReferenceError: document is not defined
  - locomotive-scroll.esm.js:151 Module../node_modules/locomotive-scroll/dist/locomotive-scroll.esm.js
    node_modules/locomotive-scroll/dist/locomotive-scroll.esm.js:151:1
  
  - index.js:1 Module../src/pages/index.js
    src/pages/index.js:1:1
  
  - interopRequireDefault.js:7 Object.Module._extensions..js
    node_modules/@babel/runtime/helpers/interopRequireDefault.js:7:1

Any thoughts? Thank you!

Cheers.

Edit: I’ve found this, but It’s not working, and this, but I’m not sure how to make it working with locomotive

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

12reactions
edoardolunardicommented, Oct 9, 2019

Solved! After tried many times with the links I posted, I’ve found a more easy way: basically when the component is mounted, import the module/library. This will fix the problem relate to ssr - see here

So

useEffect(() => {
    import("locomotive-scroll").then(locomotiveModule => {
      const scroll = new locomotiveModule.default({
        el: scrollRef.current,
        smooth: true,
        smoothMobile: false,
      })
    })
  }, [])
9reactions
ryanturnerwebdevcommented, Aug 6, 2021

FYI if you want to fix the build issue without having to load the script asynchronously, add this to gatsby-node.js

exports.onCreateWebpackConfig = ({ stage, loaders, actions }) => {
  if (stage === "build-html" || stage === "develop-html") {
    actions.setWebpackConfig({
      module: {
        rules: [
          {
            test: /locomotive-scroll/,
            use: loaders.null(),
          },
        ],
      },
    })
  }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

reactjs - WebpackError: ReferenceError: document is not defined
I am getting an error building my project on netlify (WebpackError: ReferenceError: document is not defined). In develop works fine!
Read more >
WebpackError: ReferenceError: document is not defined - IE ...
Steps to reproduce. Running gatsby build within project folder after declaring const isIE = /*@cc_on! · Expected result. Site builds without ...
Read more >
How to fix 'document not defined' in Gatsby Build
The error document is not defined or window is not defined you see when building a gatsby project is as a result of...
Read more >
How to fix ReferenceError: Document is not defined ... - Sabe.io
The most common reason for getting the reference error while on the browser is when you try to access the document object too...
Read more >
How To Fix ReferenceError document is not defined ... - Isotropic
If you are trying to use the document object and receiving a ReferenceError: document is not defined error then there is a good...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found