Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jsPDF 2.0.0 - Critical dependency: the request of a dependency is an expression #2846

Closed
lukfel opened this issue Aug 13, 2020 · 32 comments · Fixed by #2865
Closed

jsPDF 2.0.0 - Critical dependency: the request of a dependency is an expression #2846

lukfel opened this issue Aug 13, 2020 · 32 comments · Fixed by #2865

Comments

@lukfel
Copy link

lukfel commented Aug 13, 2020

Packages:
Node: 10.14.2
Angular: 10.0
jsPDF: 2.0.0

Problem:
I just updated jsPDF from 1.5.3 to 2.0.0 in my Angular 10.0 project and suddenly experience following warning during building:

WARNING in ./node_modules/jspdf/dist/jspdf.umd.min.js 195:240-254
Critical dependency: the request of a dependency is an expression

Usage:

import { jsPDF } from "jspdf";
import html2canvas from 'html2canvas';

public savePDF(fileName: string) {

    /* tslint:disable */
    window['html2canvas'] = html2canvas;
    /* tslint:enable */

    const jspdf = new jsPDF({
      orientation: 'p',
      unit: 'pt',
      format: 'a4'
    });

    const canvas = this.canvas.nativeElement;

    try {
      jspdf.html(canvas, {
        html2canvas: {
          allowTaint: true,
          useCORS: true,
          scale: 600 / canvas.scrollWidth
        },
        callback: async (doc) => {
          doc.save(`${fileName}.pdf`);
          console.log('Success');
        }
      });
    } catch (error) {
      console.error(error);
    }
  }
agmo added a commit to agmo/export-grid-data that referenced this issue Aug 13, 2020
jsPDF displays a warning when ng-serving: "Critical dependency: the request of a dependency is an expression". An issue has been reported about it: parallax/jsPDF#2846
@absoliman
Copy link

I can add that also we found the below lines.

[ng] WARNING in ./node_modules/jspdf/dist/jspdf.umd.min.js 195:141-151
[ng] Critical dependency: the request of a dependency is an expression
[ng] WARNING in ./node_modules/jspdf/dist/jspdf.umd.min.js 195:240-254
[ng] Critical dependency: the request of a dependency is an expression

@yuu2lee4
Copy link

+1

@sreeisalso
Copy link

Screenshot 2020-08-14 at 12 02 46 PM

browser logs

@HackbrettXXX
Copy link
Collaborator

Thanks for the elaborate issue report. The reason is probably how we load the optional libraries like html2canvas now. We should probably inline the code in loadOptionalLibrary so the import is no longer an expression.

If your code works, you can probably just ignore the warning for now. You might also try #2839 (comment)

@YaroslavAntipov
Copy link

+1

1 similar comment
@egluhbegovic
Copy link

+1

@Ribeiro-Tiago
Copy link

Ribeiro-Tiago commented Aug 18, 2020

If your code works (...)

@HackbrettXXX What if it doesn't ? 😛

My current issue is trying to make a PDF from HTML, and I'm getting errors from dompurify / html2canvas (tried both with HTML element and string).
image

Copied the example from the documentation, just changed the element:

import { jsPDF } from 'jspdf'

(...)

// triggered on button click
getPdf() { 
	const pdf = new jsPDF()
	
	pdf.html(document.getElementById('report'), {
	  callback: function (doc) {
	    doc.save()
	  },
	})
	
	pdf.html(document.getElementById('report').innerHTML, {
	  callback: function (doc) {
	    doc.save()
	  },
	})
}

Also tried using document.body (exactly as documentation) and

import * as jsPDF from 'jspdf'

const pdf = new jsPDF.jsPDF()

for testing sake, with no luck

I'm running
"vue": "^2.6",
"jspdf": "^2.0.0",
"dompurify": "^2.0.12",
"html2canvas": "^1.0.0-rc.7",
"core-js": "^3.0.0",

Could it be related to this issue ?

@HackbrettXXX
Copy link
Collaborator

@Ribeiro-Tiago I'm currently digging into that. Seems like build tools don't like expressions as imports. I hope I can publish a new release with a fix soon. As workaround you can try to include the UMD variant of jsPDF as script tag and ignore the warning.

@0x78f1935
Copy link

Same here

WARNING in ./node_modules/jspdf/dist/jspdf.umd.min.js
177:142-152 Critical dependency: the request of a dependency is an expression
 @ ./node_modules/jspdf/dist/jspdf.umd.min.js
 @ ./frontend/widgets/table.jsx
 @ ./frontend/views/retouren/bon-printer.jsx
 @ ./frontend/views/_backendGlue.jsx
 @ ./frontend/views/_routes.jsx

WARNING in ./node_modules/jspdf/dist/jspdf.umd.min.js
177:241-255 Critical dependency: the request of a dependency is an expression
 @ ./node_modules/jspdf/dist/jspdf.umd.min.js
 @ ./frontend/widgets/table.jsx
 @ ./frontend/views/retouren/bon-printer.jsx
 @ ./frontend/views/_backendGlue.jsx
 @ ./frontend/views/_routes.jsx

Code:

    generatePdf()
    {
        const input = document.getElementById(this.state.unique_id);  
        html2canvas(input)  
        .then((canvas) => {  
            var imgWidth = 210; 
            var pageHeight = 295;  
            var imgHeight = canvas.height * imgWidth / canvas.width;
            var heightLeft = imgHeight;
            var position = 0;

            const imgData = canvas.toDataURL('image/png');  
            const pdf = new jsPDF('p', 'mm', 'a4')  

            pdf.addImage(imgData, 'PNG', 0, position, imgWidth, imgHeight);
            heightLeft -= pageHeight;

            while (heightLeft >= 0) {
                position = heightLeft - imgHeight;
                pdf.addPage();
                pdf.addImage(imgData, 'PNG', 0, position, imgWidth, imgHeight);
                heightLeft -= pageHeight;
            }
            pdf.save(this.props.pdf_filename);
        });
    }

@Ribeiro-Tiago
Copy link

@HackbrettXXX my issue is not the warning per se, but the errors. Still happening with including as script tag. Could it be caused by some vue's webpack config ? Shall I open a new issue for this ?

@HackbrettXXX
Copy link
Collaborator

@Ribeiro-Tiago I'm pretty sure it's the same issue. I tested it yesterday with an Angular project. I got the warning and code that uses one of the optional libraries does not work. I'm pretty sure the warnings/errors are the same for Vue.

@Ribeiro-Tiago
Copy link

@HackbrettXXX oki, do you have an ETA on when you might have some time to release a fix ?

@HackbrettXXX
Copy link
Collaborator

I hope next week.

@yerycs
Copy link

yerycs commented Aug 20, 2020

Same warning. I am waiting next release. :)

HackbrettXXX added a commit to HackbrettXXX/jsPDF that referenced this issue Aug 20, 2020
- inline loadOptionalLibrary code
- remove "browser" field in pacakge.json so tools will prefer the es variant
- update Optional Dependencies section in readme
- fix rollup config

fix parallax#2846
@HackbrettXXX
Copy link
Collaborator

@RegniBlef could you check if the pull request #2865 really fixes the issue?

@Blunderchips
Copy link

Getting the same error on an Angular project with the following snippit:

const doc = new jsPDF();
doc.html('<html>Hello <b>world</b></html>');
doc.autoPrint();

Dependencies:

"core-js": "3.6.5",
"dompurify": "2.0.12",
"html2canvas": "1.0.0-rc.7",
"jspdf": "2.0.0",

Error message:

zone-evergreen.js:659 Unhandled Promise rejection: Could not load dompurify: Error: Cannot find module 'dompurify' ; Zone: <root> ; Task: Promise.then ; Value: Error: Could not load dompurify: Error: Cannot find module 'dompurify'
    at jspdf.umd.min.js:212
    at ZoneDelegate.invoke (zone-evergreen.js:364)
    at Zone.run (zone-evergreen.js:123)
    at zone-evergreen.js:857
    at ZoneDelegate.invokeTask (zone-evergreen.js:399)
    at Zone.runTask (zone-evergreen.js:167)
    at drainMicroTaskQueue (zone-evergreen.js:569) Error: Could not load dompurify: Error: Cannot find module 'dompurify'
    at http://localhost:4200/vendor.js:132143:811
    at ZoneDelegate.invoke (http://localhost:4200/polyfills.js:6463:30)
    at Zone.run (http://localhost:4200/polyfills.js:6222:47)
    at http://localhost:4200/polyfills.js:6956:40
    at ZoneDelegate.invokeTask (http://localhost:4200/polyfills.js:6498:35)
    at Zone.runTask (http://localhost:4200/polyfills.js:6266:51)
    at drainMicroTaskQueue (http://localhost:4200/polyfills.js:6668:39)

HackbrettXXX added a commit that referenced this issue Aug 21, 2020
* fix "dependency is an expression" issue

- inline loadOptionalLibrary code
- remove "browser" field in pacakge.json so tools will prefer the es variant
- update Optional Dependencies section in readme
- fix rollup config

fix #2846

* add missing file extensions to imports
@Ribeiro-Tiago
Copy link

Was going to try with the branch you mentioned earlier but saw that you just merged it to master so tried with master (cloned locally, ran npm run build moved /dist into my project's /node_modules/jspdf/dist), and it's working. Awesome job @HackbrettXXX

@HackbrettXXX
Copy link
Collaborator

Good to hear. Thanks.

I will prepare a new release next week.

@hugomejia
Copy link

Hi,

I don't if this is really relevent, but just for those who are struggling with this issue (because it blocks indeed the PDF generation), just install version jsPDF 1.5.3, works like a charm.

Just in case, I'm currently making PDFs in a VueJS app, using jsPDF@1.5.3 and html2canvas 1.0.0-rc.7.

@lukfel
Copy link
Author

lukfel commented Aug 21, 2020

@HackbrettXXX Also just tried it and found no initial problems. Thanks for the good work.

@senter-logistics
Copy link

jsPDF

working. install

npm i jspdf@1.5.3

@HackbrettXXX
Copy link
Collaborator

@e-hop or npm i jspdf@2.1.0 ;)

@TomaszWaszczyk
Copy link

If using yarn you can pin jspdf version to ^2.1.0 in your project's package.json for temporary fix.

"resolutions": {
	"material-table/jspdf": "^2.1.0"
}

@petitmat
Copy link

petitmat commented Sep 3, 2020

That's a bit strange, I still have the issue after upgrading to 2.1.0, do you know why ?

Here is my code

let doc = new jsPDF("p", "pt", "a4")
doc.html("<div><h3>Test</h3></div>", {
    callback: function (d) {
        d.output("dataurlnewwindow")
    },
})

with my package.json

"dependecies: {
    "jspdf": "^2.1.0",
    "dompurify": "^2.0.12",
    "html2canvas": "^1.0.0-rc.7",
}

error:

Uncaught (in promise) Error: Could not load dompurify: ChunkLoadError: Loading chunk 1 failed.
    at eval (jspdf.es.min.js:6779)

@HackbrettXXX
Copy link
Collaborator

@petitmat Which framework build tool do you use? Is the dompurify chunk in the dist folder? Any warnings during the build? Is dompurify correctly installed (check the node_modules folder)?.

@petitmat
Copy link

petitmat commented Sep 3, 2020

@HackbrettXXX thank you for your answer
I'm using react with webpack. In fact dompurify is not in the dist folder (in node_modules/jspdf/dist). But I don't have warnings during the build, and dompurify correctly installed (I've made a fresh npm install after I removed manually jspdf, html2canvas and dompurify from node_modules).

@HackbrettXXX
Copy link
Collaborator

That's odd. Could you share a small repro project?

@staniszmatt
Copy link

staniszmatt commented Sep 4, 2020

Hope this will this will help, below resolved the issue during the build phase and dev:
I am using React-Electron boiler plate and in the configs/webpack.config.base.js file I added to externals:

     externals: [ 
         ...Object.keys(externals || {}),
         { 'electron-debug': 'electron-debug' },
         { 'jspdf': 'jspdf' }
     ],

For both the electron warnings and the jspdf warnings appear to be resolved.

@HackbrettXXX
Copy link
Collaborator

@staniszmatt this looks like you're importing jsPDF "twice" - once as script tag (or similar, don't know how electron works in particular) and once with import statements. If that's the case you can get rid of the script tag and the externals declaration.

@jaiho8816
Copy link

I also get an errors from webpack, using jspdf
`WARNING in ./node_modules/html2canvas/dist/html2canvas.js
Failed to parse source map ...\node_modules\src\render\vector.ts

In my webpack.config.js
// webpack...
externals: {
canvg: "canvg",
html2canvas: "html2canvas",
dompurify: "dompurify"
},`

please give some solutions

@HackbrettXXX
Copy link
Collaborator

@jaiho8816 please see #3356.

@souljorje
Copy link

souljorje commented Aug 18, 2022

For rollup add external: ['canvg'] to config.

Docs: https://rollupjs.org/guide/en/#warning-treating-module-as-external-dependency

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.