jsPDF: WebPack/TypeScript: jspdf_1.default is not a constructor

I am using jsPdf in the version that is currently published via NPM in an Angular2 app written in Typescript.

We have the following code:

import jsPDF from 'jspdf'; // this imports jspdf.debug.js
var doc = new jsPDF(); // this throws an exception
doc.text('Hello world!', 10, 10)
doc.save('a4.pdf')

When executing this we are getting the following exception: jspdf_1.default is not a constructor. I can work around the issue by patching the jsPdf.debug file as described here: https://github.com/MrRio/jsPDF/issues/582 and additionally adding this line: jsPDF.default = jsPDF;

In addition I tried the latest files in the dist folder of the master branch. With those files, I need to add this line at the end of jspdf.debug.js:

 })(typeof self !== "undefined" && self || typeof window !== "undefined" && window || undefined);
  jsPDF.default = jsPDF; // new
  return jsPDF;

Is there any workaround without changing the files in the node_modules folder?

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 2
  • Comments: 39 (1 by maintainers)

Most upvoted comments

import * as jsPDF from ‘jspdf’; works for me

@AlbertoNava0307 Try : new jsPDF.default();

Nevermind… import jsPDF from 'jspdf'

worked with create-react-app

In my case I had issue lazy loading jsPDF with webpack :

import(/* webpackChunkName: "jspdf" */ 'jspdf').then(jsPDF => {
// Without lazy loading : 
var pdf = new jsPDF('landscape', 'mm', format);
// Now with lazy loading :
var pdf = new jsPDF.default('landscape', 'mm', format);
});

const { jsPDF } = require(“jspdf”);

methods: { download() { let pdfName = ‘test’; var doc = new jsPDF(); doc.text(“Hello World”, 10, 10); doc.save(pdfName + ‘.pdf’); }

finally worked for me in Vue

I was using import { jsPDF } from 'jspdf';

and had the error message :

ERROR TypeError: jspdf__WEBPACK_IMPORTED_MODULE_3__.jsPDF is not a constructor

Changing the import to : import * as jsPDF from 'jspdf';

Fixed the error for me 👍

@AlbertoNava0307 Try : new jsPDF.default();

Thanks it works

@nboisteault Is this an issue we have to solve?

Seems been solved and part of readme https://github.com/MrRio/jsPDF#angular-configuration

I’m getting the same error, ‘jspdf_1.default is not a constructor’, under the same conditions, using jsPdf version 1.3.5 currently published via NPM with Angular app written in Typescript. I have cleared my node_modules to make sure I’m pulling the latest but error is still there.