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.

ReferenceError: window is not defined in NodeJs

See original GitHub issue

Hi,i try to use jsPDF in Node but fount the error : ReferenceError: window is not defined

Are you using the latest version of jsPDF? Yes,i use "jspdf": "^1.4.1" Have you tried using jspdf.debug.js? Yes Steps to reproduce the environment : node 8.11.3

the code:

let jsPDF = require('jspdf');


var doc = new jsPDF()

doc.text('Hello world!', 10, 10)
doc.save('a4.pdf')

the error :

d:\Users\user\js\hello\node_modules\jspdf\dist\jspdf.min.js:171
Y=$.API,K=J=X="",Y.addMetadata=function(t,e){return J=e||"http://jspdf.default.namespaceuri/",X=t,this.internal.events.subscribe("postPutResources",function(){if(X){var t='<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"><rdf:Description rdf:about="" xmlns:jspdf="'+J+'"><jspdf:metadata>',e=unescape(encodeURIComponent('<x:xmpmeta xmlns:x="adobe:ns:meta/">')),n=unescape(encodeURIComponent(t)),r=unescape(encodeURIComponent(X)),i=unescape(encodeURIComponent("</jspdf:metadata></rdf:Description></rdf:RDF>")),o=unescape(encodeURIComponent("</x:xmpmeta>")),a=n.length+r.length+i.length+e.length+o.length;K=this.internal.newObject(),this.internal.write("<< /Type /Metadata /Subtype /XML /Length "+a+" >>"),this.internal.write("stream"),this.internal.write(e+n+r+i+o),this.internal.write("endstream"),this.internal.write("endobj")}else K=""}),this.internal.events.subscribe("putCatalog",function(){K&&this.internal.write("/Metadata "+K+" 0 R")

ReferenceError: window is not defined
    at d:\Users\user\js\hello\node_modules\jspdf\dist\jspdf.min.js:171:8032
    at vt (d:\Users\user\js\hello\node_modules\jspdf\dist\jspdf.min.js:1:146)
    at Object.<anonymous> (d:\Users\user\js\hello\node_modules\jspdf\dist\jspdf.min.js:1:210)
    at Module._compile (module.js:652:30)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Module.require (module.js:596:17)
    at require (internal/module.js:11:18)
Waiting for the debugger to disconnect...
ReferenceError: window is not defined
jspdf.min.js:171
    at d:\Users\user\js\hello\node_modules\jspdf\dist\jspdf.min.js:171:8032
    at vt (d:\Users\user\js\hello\node_modules\jspdf\dist\jspdf.min.js:1:146)
    at Object.<anonymous> (d:\Users\user\js\hello\node_modules\jspdf\dist\jspdf.min.js:1:210)
    at Module._compile (module.js:652:30)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Module.require (module.js:596:17)
    at require (internal/module.js:11:18)

when i use jspdf.debug.js


d:\Users\user\js\hello\node_modules\jspdf\dist\jspdf.debug.js:11776
  }(typeof self !== "undefined" && self || typeof window !== "undefined" && window || window.content || window));
                                                                                      ^

ReferenceError: window is not defined
    at d:\Users\user\js\hello\node_modules\jspdf\dist\jspdf.debug.js:11776:87
    at _typeof (d:\Users\user\js\hello\node_modules\jspdf\dist\jspdf.debug.js:2:83)
    at Object.<anonymous> (d:\Users\user\js\hello\node_modules\jspdf\dist\jspdf.debug.js:5:2)
    at Module._compile (module.js:652:30)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Module.require (module.js:596:17)
    at require (internal/module.js:11:18)
Waiting for the debugger to disconnect...
ReferenceError: window is not defined
jspdf.debug.js:11776
    at d:\Users\user\js\hello\node_modules\jspdf\dist\jspdf.debug.js:11776:87
    at _typeof (d:\Users\user\js\hello\node_modules\jspdf\dist\jspdf.debug.js:2:83)
    at Object.<anonymous> (d:\Users\user\js\hello\node_modules\jspdf\dist\jspdf.debug.js:5:2)
    at Module._compile (module.js:652:30)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Module.require (module.js:596:17)
    at require (internal/module.js:11:18)

Ideally a link too. Try fork this http://jsbin.com/rilace/edit?html,js,output

What I saw

What I expected

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:6
  • Comments:11

github_iconTop GitHub Comments

4reactions
sundaygurucommented, May 15, 2019

For those using jspdf with server rendering: I fixed this issue by using require instead of import statement and wrapped in a function. e.g

function savePdf() {
      const jsPDF = require('jspdf')
      const pdf = new JSPDF('p', 'mm', 'a4')
      pdf.addImage(imgBlob, 'PNG', 0, 0)
      pdf.save('test.pdf')
}
0reactions
itsgratiencommented, Jun 23, 2019

@adabutch, me i am using this package in node js but still not working,

Read more comments on GitHub >

github_iconTop Results From Across the Web

How To Solve ReferenceError window is not defined in ...
js application and still receive this error, it is because the window object does not actually exist in a Node.js environment. To resolve...
Read more >
ReferenceError : window is not defined at object. <anonymous ...
In my web I have 3 JavaScript files : client.js , server.js , myModule.js . In client.js I create a window variable called...
Read more >
[Solved] ReferenceError : window is not defined - ItsJavaScript
The ReferenceError : window is not defined error mainly occurs if you are using the window object in Node.js, React.js, Next.js.
Read more >
referenceerror: window is not defined, how to solve
Here's how to fix the “referenceerror: window is not defined” error that you might have in Node.js or with a tool like Next.js....
Read more >
window is not defined. This error mostly comes in node js…
This error mostly comes in node js application at the time when you are trying to run browser object in server side. Example:...
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