Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebpackError: jQuery requires a window with a document #3564

Closed
meiskv opened this issue Jan 17, 2018 · 6 comments
Closed

WebpackError: jQuery requires a window with a document #3564

meiskv opened this issue Jan 17, 2018 · 6 comments

Comments

@meiskv
Copy link

meiskv commented Jan 17, 2018

`import gsap, { TweenMax } from 'gsap'

const $ = require('jQuery');

(function($){

var introText = $('.intro_txt');

TweenMax.to(introText,1,{opacity: 0.1, delay:1})

})($)`

I tried this solution that I found but It doesn't work.

import jsdom from 'jsdom'; global.document = new jsdom.JSDOM(...); const $ = jquery(global.document.window);

These errors happen on gatsby build

@markmichon
Copy link
Contributor

Have you tried the details in Debugging HTML Builds? I can't speak to the jsdom workaround you posted.

Essentially you want Gatsby's build process to ignore the browser-only javascript (like accessing window). The easiest solution for most cases is wrapping that code in a check for the window object as shown in the doc link.

@jonbellah
Copy link

To tack on to what @markmichon said, this error happens because Gatsby does server rendering and the window object does not exist on the server. The window check conditional in the docs is your answer.

@meiskv
Copy link
Author

meiskv commented Jan 17, 2018

I already fixed it. But is it okay to wrap not only the required module but the whole script for that reason? Thank you so much @markmichon & @jonbellah .

@jonbellah
Copy link

It depends on the module (and I can't necessarily tell the business case for the above code, so it's up to you). But generally, what happens when you check for the presence of the window object is it excludes that chunk/module from the SSR'd code, meaning most search engines (Google will parse JS, but there's no good evidence that others do) will not see it, nor will users who have disabled JS.

Once the server renders the initial chunk to a string and pushes it out to the client, the client parses it and loads the additional JS (which will load in your greensock stuff).

@jonbellah
Copy link

@KyleAMathews This issue appears to be resolved. Mind closing? Thanks!

@KyleAMathews
Copy link
Contributor

Done :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants