Make sure you include the script from vite before you own script
Feb 20, 2023
10
Level 63
Add chart.js to Laravel with npm
Hello,
I have installed chart.js with npm and I try to use it in a view.
I have added this in the app.js file.
import Chart from 'chart.js/auto';
window.Chart = Chart;
And then I have added this a the view.
<section>
<canvas id="myChart"></canvas>
</section>
...
<script>
const ctx = document.getElementById('myChart');
new Chart(ctx, {
type: 'bar',
data: {
labels: ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'],
datasets: [{
label: '# of Votes',
data: [12, 19, 3, 5, 2, 3],
borderWidth: 1
}]
},
options: {
scales: {
y: {
beginAtZero: true
}
}
}
});
</script>
I get this error in the console.
Uncaught ReferenceError: Chart is not defined
I have try with window.Chart but I get the same error with window.Chart not defined.
Any idea ?
Thanks ;).
V
Level 102
@vincent15000 try telling your own script to be a module
<script type="module" >
2 likes
Please or to participate in this conversation.