# P5.JS & jsPDF cant use 2.4.0 but 1.3.3 works

**URL:** https://discourse.processing.org/t/p5-js-jspdf-cant-use-2-4-0-but-1-3-3-works/33792
**Category:** Libraries
**Created:** [November 28, 2021, 4:29am UTC](https://discourse.processing.org/t/p5-js-jspdf-cant-use-2-4-0-but-1-3-3-works/33792 "2021-11-28T04:29:43Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![24601](https://avatars.discourse-cdn.com/v4/letter/2/8e8cbc/32.png) [@24601](https://discourse.processing.org/u/24601)
#### Post date: [November 28, 2021, 4:29am UTC](https://discourse.processing.org/t/p5-js-jspdf-cant-use-2-4-0-but-1-3-3-works/33792/1 "2021-11-28T04:29:43Z")

</div>

Hello

Trying to upgrade jsPDF library to the latest version but

Currently using this

```auto
<script src="//cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.3/jspdf.min.js"></script>

```

Swapping it out for this

```auto
<script src="https://unpkg.com/jspdf@latest/dist/jspdf.umd.min.js"></script>

```

I get:

```auto
Uncaught ReferenceError: jsPDF is not defined

```

Any help would be great,

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [November 28, 2021, 5:45am UTC](https://discourse.processing.org/t/p5-js-jspdf-cant-use-2-4-0-but-1-3-3-works/33792/2 "2021-11-28T05:45:30Z")

</div>

> [@24601](#):
>
> `Uncaught ReferenceError: jsPDF is not defined`

To test if jsPDF library is fully loading I’ve created this “index.html” file:

```auto
<script async src=https://cdn.JsDelivr.net/npm/p5></script>
<script defer src=https://Unpkg.com/jspdf/dist/jspdf.umd.min.js></script>
<!-- <script defer src=sketch.js></script> -->

<script>
  onload = function () {
    console.log('p5js:', globalThis.p5);
    console.log('jsPDF:', globalThis.jsPDF);
    console.log('jspdf:', globalThis.jspdf);
    console.log('jspdf.jsPDF:', globalThis.jspdf.jsPDF);
  }
</script>

```

Indeed jsPDF is `undefined`. But interestingly jspdf exists!

And also there’s a jsPDF attached to jspdf.

BtW, you can create a global variable to directly access jsPDF like this:  
`const { jsPDF } = jspdf;`

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [November 28, 2021, 7:27am UTC](https://discourse.processing.org/t/p5-js-jspdf-cant-use-2-4-0-but-1-3-3-works/33792/3 "2021-11-28T07:27:05Z")

</div>

Seems like this is a common case for UMD packed files.

That is, they have a namespace object which wraps up their actual main constructor.

For the jsPDF library we have a namespace jspdf and a main constructor jsPDF inside jspdf.

I had to do the same for the ml-matrix library:

> [@Autocomplete and documentation for "imported" p5js](https://discourse.processing.org/t/autocomplete-and-documentation-for-imported-p5js/31927/14):
>
> When we use import in JS 2 things happen: The file becomes a JS module and it needs a \<script type=module\> to load inside the HTML. JS checks the import line if a file w/ that exactly path & name exists so it can load it as a module. As we can note the file “ml-matrix” doesn’t exist. Instead there are “matrix.js” & “matrix.umd.js”. However neither is a JS module but just vanilla JS code. The library’s types is called “matrix.d.ts”. But to make things worse “matrix.umd.js” is exposed as …

In its UMD format ml-matrix has a namespace mlMatrix and a constructor Matrix inside mlMatrix:

> <https://github.com/GoToLoop/ML-Matrix-P5JS-Template/blob/v1.0.6/sketch.js#L10-L11>

---

<div class="post-metadata">

### Author: ![24601](https://avatars.discourse-cdn.com/v4/letter/2/8e8cbc/32.png) [@24601](https://discourse.processing.org/u/24601)
#### Post date: [November 28, 2021, 7:55am UTC](https://discourse.processing.org/t/p5-js-jspdf-cant-use-2-4-0-but-1-3-3-works/33792/4 "2021-11-28T07:55:19Z")

</div>

> [@GoToLoop](#):
>
> `const { jsPDF } = jspdf;`

Awesome thanks for your help it’s now working
