0

I'm using Chartjs for the first time, and I'm having a problem with the import. When I was building my chart it didn't recognize "Chart" which led me to import it, but now it gives me the following error: "Cannot use import statement outside a module". If you can help me I would be extremely grateful.

My code:

<script src="node_modules/chart.js/dist/chart.min.js"></script>

 <script>
     import Chart from 'chart.js';


     var ctx = document.getElementsByClassName('line-chart');

     var chartGraph = new Chart(ctx, {
         type: 'line',
         date: {
             labels: ["Jan", "Feb", "Mar"],
             datasets: [{
                 label: 'Rate',
                 date: [5,10,5,14,20]
             }]
         }
     });
 </script>

<canvas class="line-chart"></canvas>`
steep88
  • 21
  • 2
  • 1
    Does this answer your question? ["Uncaught SyntaxError: Cannot use import statement outside a module" when importing ECMAScript 6](https://stackoverflow.com/questions/58211880/uncaught-syntaxerror-cannot-use-import-statement-outside-a-module-when-import) – Daniel Beck Mar 11 '23 at 15:21

1 Answers1

-1

You are getting this error because the import statement can only be used within modules, and the script tag you are using does not create a module.

You can use a module bundler like webpack to bundle your code and chart.js library into a single file