最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - SyntaxError: Cannot use import statement outside a module -- jspdf - Stack Overflow

programmeradmin1浏览0评论

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="@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>

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./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>
Share Improve this question asked May 17, 2021 at 10:15 Geordi tyrrellGeordi tyrrell 711 silver badge2 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

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./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>

发布评论

评论列表(0)

  1. 暂无评论