Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

distante's avatar

ApexCharts with NPM and Vite

Hey!

So I've installed ApexCharts with NPM and I'm using Vite as my asset bundler. When I run vite build it runs successfully, I get no errors. However, when I visit a page that uses a chart, I get the following error in the console:

(index):482 Uncaught ReferenceError: ApexCharts is not defined

I've tried a few different ways to try and get it to work, but below is my current attempt:

app.js

import './bootstrap';

import ApexCharts from 'apexcharts';
window.ApexCharts = ApexCharts;

index.blade.php

<div>
    <div id="chart" class="w-96"></div>
</div>

<script>
    const chart = new ApexCharts(document.querySelector('#chart'), {
        // Chart options here
        series: [{
            name: 'Series 1',
            data: [30, 40, 35, 50, 49, 60, 70, 91, 125]
        }],
        chart: {
            type: 'line',
            height: 350
        },
        xaxis: {
            categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep']
        }
    });

    chart.render();
</script>

vite.config.js

import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';

export default defineConfig({
    plugins: [
        laravel({
            input: [
                'resources/css/app.css',
                'resources/js/app.js',
            ],
            refresh: true,
        }),
    ],
    build: {
        chunkSizeWarningLimit: 1600
    }
});

What have I missed out to get my chart to render? I feel like it's something missing from the Vite config.

0 likes
7 replies
gych's avatar

Try to use

 const chart = new window.ApexCharts(document.querySelector('#chart'), {
1 like
distante's avatar

@gych Thanks, now seem to be getting a different issue though

(index):417 Uncaught TypeError: window.ApexCharts is not a constructor
gych's avatar

@distante Ok I see it won't work as a constructor then its better to import apex chart directly where you use it and remove it from app.js

Try like this:

<script>
	import ApexCharts from 'apexcharts';

    const chart = new ApexCharts(document.querySelector('#chart'), {
1 like
distante's avatar

@gych It's not a fan of that, it got rid of the constructor error and imported it where it's used, but now I have

Uncaught SyntaxError: Cannot use import statement outside a module
gych's avatar
gych
Best Answer
Level 29

@distante Ok try to replace <script> with <script type="module"> this should allow ES6 module syntax

1 like
distante's avatar

@gych Awesome, thanks!

I replaced the script tag and put the import back into app.js and it works! I think the script tag bit was the only step I was missing!

Please or to participate in this conversation.