Window is not defined at build time with Gatbsy
See original GitHub issueHi there,
so the problem that I’m facing is, that the window object is used by this library and that it is not available on build time with a static-site-generator like Gatbsy. Same goes for the main jsPDF library. Added also an issue for it here: https://github.com/MrRio/jsPDF/issues/2697
I got it to work like this for now: https://gist.github.com/henrikwirth/71a214373b00f7e2af0e6a0fe67547db#file-jspdf-utils-js-L3-L4
const jsPDF = require("jspdf")
require("jspdf-autotable")
And before I call the class I do something like this:
export const isBrowser = typeof window !== `undefined`
const createPdf = () => {
if (!isBrowser) return
const pdf = new PDF("MyPDF", { defaultFontSize: 10, debug: false })
}
Just wondering if it would be possible to do the window check in the library directly so that it is easier to import it. Any thoughts?
Issue Analytics
- State:
- Created 4 years ago
- Comments:10 (5 by maintainers)
Top Results From Across the Web
Gatsby - the window is not defined error, what and how to fix it?
Check if window is defined. You can check if the browser-global object window is defined and based on it, execute the related code...
Read more >Window is not defined · Issue #309 · gatsbyjs/gatsby - GitHub
I'm trying to use a semantic-ui import along with my components. Works fine in development but if I try to gatsby build ,...
Read more >Error when building Gatsby website: window is not defined
!== 'undefined' condition. Essentially, this is because gatsby build occurs in the server, where there's no window (or other global objects such ...
Read more >Debugging HTML Builds | Gatsby
Errors while building static HTML files (the build-time React SSR ... If this is your problem you should see an error above like...
Read more >Window is not defined in gatsby build : r/gatsbyjs - Reddit
Hi Everybody, i have a gatsby v3.10.1 app template and it's running perfectly, but when i try to do "gatsby build" it's not...
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
Yeah. I think autoTable now supports gatsby SSR as soon as jspdf is updated.
Yup, like I said it is a problem of both libraries. I just tried the build with your example and got this one, which makes sense as I imported both the normal way.
I’ll be happy to test your change. Is it already released over npm?