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

UniTue's avatar

import ApexCharts from 'apexcharts'; Not working

Hey there,

this weekend I tried to install ApexCharts and html2canvas via npm.

After a successful installtion, I added the packages to my app.js: import ApexCharts from 'apexcharts';

Sadly this did not work. So I tried different things.. I could see that the *.js file size went up, everytime I added a line to my app.js. But I couldn't access the objects.. in this case for example:ApexCharts.

After adding following line to my app.js it worked: window.ApexCharts = ApexCharts;

Right now, there are 2 Charts displayed (both look the same). Can u tell me why I need the window.ApexCharts = ApexCharts Mapping?

This doen't seem to be right.

Thank you in advance.

0 likes
4 replies
LaryAI's avatar
Level 58

The reason you need to assign ApexCharts to window.ApexCharts is because the library is not being exported as a global variable by default. By assigning it to window.ApexCharts, you are making it accessible globally throughout your application.

Here's an example of how you can import and use ApexCharts in your app.js file:

import ApexCharts from 'apexcharts';

// Create a new chart instance
const chart = new ApexCharts(document.querySelector("#chart"), options);

// Render the chart
chart.render();

However, if you want to access ApexCharts globally without assigning it to window.ApexCharts, you can use a module bundler like Webpack or Rollup to create a global variable for it. Here's an example using Webpack:

  1. Install the expose-loader package:
npm install expose-loader --save-dev
  1. Update your webpack.config.js file to include the expose-loader:
module.exports = {
  // ...
  module: {
    rules: [
      // ...
      {
        test: require.resolve('apexcharts'),
        use: [{
          loader: 'expose-loader',
          options: 'ApexCharts'
        }]
      }
    ]
  }
};
  1. Now you can import and use ApexCharts globally without assigning it to window.ApexCharts:
import 'apexcharts';

// Create a new chart instance
const chart = new ApexCharts(document.querySelector("#chart"), options);

// Render the chart
chart.render();

Remember to adjust the #chart selector to match the ID of the element where you want to render the chart.

I hope this helps! Let me know if you have any further questions.

UniTue's avatar

Larys answer is quite good. Except I don't understand why all install instructions only mention adding: import ApexCharts from 'apexcharts';

And I never read to assign the variable global? Is that something I missed? Is it a problem when using @push with javascript code inside the blade.php?

Can someone explain this behavior to me?

Thank you in advance.

thinkverse's avatar
Level 15

I don't understand why all install instructions only mention adding: import ApexCharts from 'apexcharts';

@UT I'm just guessing here, but it could be that most npm packages expect you to work with a front-end framework like React or Vue. And there everything is bundled into a single JavaScript file, for the most part. Which again, for the most part, doesn't need to access objects on the global scope.

1 like
UniTue's avatar

@thinkverse Thank you, that makes sense! Would love to have something like this documented and readable for everyone.

Please or to participate in this conversation.