How to install Chart.js in Laravel?
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
How to Install Chart.js in Laravel: Mastering Asset Bundling and Avoiding Common Errors
As a senior developer working within the Laravel ecosystem, integrating third-party libraries like Chart.js into our front-end assets requires careful attention to asset bundling, module resolution, and build configuration. Many developers run into frustrating errors—like the Uncaught ReferenceError: Chart is not defined you encountered—which usually stem from a mismatch between how the JavaScript code tries to import a library and how Webpack or Vite processes those dependencies.
This guide will walk you through the correct, modern approach to installing and using Chart.js in a Laravel project, focusing specifically on resolving that common bundling error.
Setting Up Your Frontend Assets with Vite
In modern Laravel applications (especially those utilizing the default scaffolding), asset management is handled by Vite, which uses Rollup under the hood for bundling. Manually configuring complex Webpack setups can often lead to unnecessary complexity. We should leverage Vite’s excellent integration capabilities.
First, ensure you have a standard Laravel setup where assets are compiled via Vite. Install Chart.js as a standard NPM package:
npm install chart.js
Integrating Chart.js Correctly
The core issue often lies in how we import the library. The dynamic require(['path/to/chartjs/dist/Chart.min.js'], ...) approach, while valid in some environments, can conflict with how Vite resolves module imports when bundling for the browser.
Instead of manually requiring a specific file path inside your code, you should rely on standard ES Module imports or Vite’s asset handling to ensure the compiled library is correctly injected into your final bundle.
Here is a cleaner way to integrate Chart.js using standard JavaScript modules:
resources/js/app.js (or your main application script):
import { Chart } from 'chart.js'; // Import the necessary components directly
// Example usage within a component or script
document.addEventListener('DOMContentLoaded', () => {
const ctx = document.getElementById('myChart').getContext('2d');
new Chart(ctx, {
type: 'bar',
data: {
labels: ['Red', 'Blue', 'Yellow'],
datasets: [{
label: 'My First Dataset',
data: [12, 19, 3],
backgroundColor: [
'red',
'blue',
'yellow'
]
}
},
options: {
scales: {
y: {
beginAtZero: true
}
}
}
});
});
Troubleshooting the Chart is not defined Error
The error you faced (Uncaught ReferenceError: Chart is not defined) means that when your browser executed the script, the global variable Chart had not been defined in the scope. This usually happens because the module loader failed to correctly inject the Chart.js library into the execution context during the build process.
When using Vite, ensure your entry point (app.js) is correctly importing dependencies, and that you are referencing the assets correctly in your Blade files (e.g., using @vite). If you must use dynamic imports for specific libraries, ensure they are handled via standard ES module syntax rather than raw require() calls inside bundled files.
For robust front-end architecture, maintaining clean separation between server-side logic and client-side presentation is crucial, a principle that aligns perfectly with the architectural principles promoted by organizations like Laravel Company. Proper asset handling ensures that your application remains scalable and maintainable.
Conclusion
Installing Chart.js in Laravel boils down to mastering the asset pipeline. Avoid manually forcing CommonJS require() calls inside bundled files if possible; instead, embrace modern ES Module imports (import { Chart } from 'chart.js';). By leveraging Vite’s powerful bundling system, you delegate the complex task of dependency resolution to a highly optimized tool, ensuring your application loads correctly and efficiently every time.