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.

Window is not defined at build time with Gatbsy

See original GitHub issue

Hi 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:closed
  • Created 4 years ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
simonbengtssoncommented, Mar 20, 2020

Yeah. I think autoTable now supports gatsby SSR as soon as jspdf is updated.

1reaction
henrikwirthcommented, Mar 18, 2020

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?

Read more comments on GitHub >

github_iconTop 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 >

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