Geordi tyrrell
Geordi tyrrell

Reputation: 71

SyntaxError: Cannot use import statement outside a module -- jspdf

I have been trying to create a html and javascript application that uses the jspdf module to export pdf files.

Everytime i run the code bellow I get the error Uncaught SyntaxError: Cannot use import statement outside a module

    <!DOCTYPE html>
    <html>
    <head>
        <title>Test 2</title>
        <script src="https://unpkg.com/jspdf@latest/dist/jspdf.umd.min.js">
            
        </script>
            
        <script type="text/javascript">
            import { jsPDF } from "jspdf";
    
            // Default export is a4 paper, portrait, using millimeters for units
            const doc = new jsPDF();
    
            doc.text("Hello world!", 10, 10);
            doc.save("a4.pdf");
            alert("hello")
        </script>
    
    
    </head>
    <body>
    
    </body>
    </html>

Upvotes: 7

Views: 3371

Answers (1)

Ernesto Stifano
Ernesto Stifano

Reputation: 3130

See "Other Module Formats" > "Globals" in the NPM Package Page.

You can't use import statement outside an ES6 module. Generally the script tag has type attribute set to module

<!DOCTYPE html>
    <html>
    <head>
        <title>Test 2</title>
        <script src="https://unpkg.com/jspdf@latest/dist/jspdf.umd.min.js">
            
        </script>
            
        <script type="text/javascript">    
            // Default export is a4 paper, portrait, using millimeters for units
            const { jsPDF } = window.jspdf;
            
            const doc = new jsPDF();
    
            doc.text("Hello world!", 10, 10);
            doc.save("a4.pdf");
            alert("hello")
        </script>
    
    
    </head>
    <body>
    
    </body>
    </html>

Upvotes: 7

Related Questions