apexcharts.rb: [BUG] Uncaught ReferenceError: ApexCharts is not defined

I’ve trying use ApexCharts in my RoR 6 app, but occurred the error below:

image

My Gemfile:

source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby '2.6.3'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 6.0.0'
# Use Puma as the app server
gem 'puma', '~> 3.11'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5'
# Transpile app-like JavaScript. Read more: https://github.com/rails/webpacker
gem 'webpacker', '~> 4.0'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.7'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 4.0'
# Use Active Model has_secure_password
gem "bcrypt", "~> 3.1"

gem 'apexcharts', require: 'apexcharts/prefix_with_apex'
gem 'chartkick'
gem 'simple_form'
# Use Active Storage variant
# gem 'image_processing', '~> 1.2'

# Reduces boot times through caching; required in config/boot.rb
gem 'bootsnap', '>= 1.4.2', require: false

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
  # Use sqlite3 as the database for Active Record
  gem 'sqlite3', '~> 1.4'
end

group :development do
  # Access an interactive console on exception pages or by calling 'console' anywhere in the code.
  gem 'listen', '>= 3.0.5', '< 3.2'
  gem 'rubocop', require: false
  gem 'web-console', '>= 3.3.0'
  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'
  gem 'spring-watcher-listen', '~> 2.0.0'
end

group :test do
  # Adds support for Capybara system testing and selenium driver
  gem 'capybara', '>= 2.15'
  gem 'selenium-webdriver'
  # Easy installation and use of web drivers to run system tests with browsers
  gem 'webdrivers'
end

group :production do
  gem 'pg'
  gem 'rails_12factor'
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

My app/javascript/packs/application.js

require("@rails/ujs").start();
require("turbolinks").start();
require("@rails/activestorage").start();
require("channels");
require("chartkick");
require("chart.js");
require("apexcharts");

import "bootstrap"
import "../stylesheets/application"

const feather = require('feather-icons')

document.addEventListener("turbolinks:load", () => {
    feather.replace();
});

My app is built in: Ruby 2.6.3 Rails 6 Webpacker

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 17 (11 by maintainers)

Most upvoted comments

have you try ?

//AMD
window.ApexCharts = require('apexcharts');
console.log(window.ApexCharts); // should return modules

// or
// Imports
import ApexCharts from 'apexcharts';
window.ApexCharts = ApexCharts; // return apex chart 

based on the apexcharts.js in order to import it you need to define the variables for it first and you need to expose it via window.ApexCharts in order to use it outside webpack bundle

Thankss

will need to update the documentation regarding webpack bundle the current require('apexcharts') is worked if not using webpack (scope issue)

I’m having the same problem Uncaught ReferenceError: ApexCharts is not defined, but in my case using importmap

app/javascript/application.js

// Configure your import map in config/importmap.rb. Read more: https://github.com/rails/importmap-rails
import "@tabler/core"
import "@hotwired/turbo-rails"
import "controllers"

import Cleave from "cleave.js"
import jQuery from "jquery"
import 'jquery.signaturepad'

import ApexCharts from 'apexcharts'
window.ApexCharts = ApexCharts

importmap.rb

...
pin "apexcharts", to: "https://ga.jspm.io/npm:apexcharts@3.37.3/dist/apexcharts.common.js", preload: true

Thank you @styd that worked, I can work on a pull request to update the readme.