document is not defined
See original GitHub issueHi 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:
- Created 7 years ago
- Comments:7
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Working for me.
Try install
npm i -S gsap
Edit in filewebpack.base.config.js