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.

document is not defined

See original GitHub issue

Hi guys!

I’m trying to add TweeMax.js from GSAP on a project with the same SSR and webpack configuration as this one ( I did copy everything for testing purposes ). When I run npm run dev it generates de client and vendor bundles perfectly, but when I access any route it throws the following exception:

/Users/vitor/Sites/ssr-test/front-end/server.js:105
        throw err
        ^

ReferenceError: document is not defined
    at Function.<anonymous> (/Users/vitor/Sites/ssr-test/front-end/node_modules/gsap/src/uncompressed/TweenMax.js:2571:11)
    at check (/Users/vitor/Sites/ssr-test/front-end/node_modules/gsap/src/uncompressed/TweenMax.js:5986:61)
    at new Definition (/Users/vitor/Sites/ssr-test/front-end/node_modules/gsap/src/uncompressed/TweenMax.js:6010:10)
    at window._gsDefine (/Users/vitor/Sites/ssr-test/front-end/node_modules/gsap/src/uncompressed/TweenMax.js:6015:12)
    at Array.<anonymous> (/Users/vitor/Sites/ssr-test/front-end/node_modules/gsap/src/uncompressed/TweenMax.js:2524:11)
    at /Users/vitor/Sites/ssr-test/front-end/node_modules/gsap/src/uncompressed/TweenMax.js:7791:9
    at Object.<anonymous> (/Users/vitor/Sites/ssr-test/front-end/node_modules/gsap/src/uncompressed/TweenMax.js:7802:3)
    at Module._compile (module.js:556:32)
    at Object.Module._extensions..js (module.js:565:10)
    at Module.load (module.js:473:32)

I did some research, and I think it’s because we don’t have document or window global objects on the server side environment. I tried to fix it declaring both as global variables on the server-entry.js file, but it was unsuccessful

if (typeof(window) == 'undefined'){
    let dummyElement = { style: {}, getElementsByTagName: function() { return [] } }
    global.window = {}
    global.navigator = { userAgent: "" }
    global.document = { createElement: function() { return dummyElement } }
}

Maybe anyone can help me with that? I’m probably missing something.

Thanks!

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:7

github_iconTop GitHub Comments

3reactions
ktquezcommented, Dec 16, 2016

Working for me.

require('jsdom-global')(html, {
  url: `http://${process.env.HOST}:${process.env.PORT}`
})
2reactions
nghiepdevcommented, Jan 4, 2017

Try install npm i -S gsap Edit in file webpack.base.config.js

entry: {
    app: './src/client-entry.js',
    vendor: [
      .....
      'gsap',
      .....
    ]
  },
Read more comments on GitHub >

github_iconTop Results From Across the Web

ReferenceError: document is not defined in JavaScript
The "ReferenceError: document is not defined" error occurs for multiple reasons: ... The document relates to the document object which represents a web...
Read more >
ReferenceError: document is not defined (in plain JavaScript)
Explanation: The error is caused because NextJs renders the page in the server only and in the server document (document is used inside...
Read more >
How to fix ReferenceError: Document is not defined in ...
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 solve the document is not defined error
Here's how to fix the “referenceerror: document is not defined” error that you might have in Node.js or with a tool like Next.js....
Read more >
How To Fix ReferenceError document is not defined in ...
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