Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

211
Vistas
Proper way to import Chart.js in CommonJS

I've been having trouble getting chart.js to import properly in a CommonJS application.

From what I can see with the docs all that seems to be required to use Chart.js with CommonJS is to simply run:

var Chart = require('chart.js');
var myChart = new Chart(ctx, {...});

However, when I run this I receive the error: "Chart is not a constructor". Inspecting the Chart object, it appears that all the modules are saved in the Chart object as properties. No big deal, I was able to get it to reference properly by doing the following:

var chartjs = require('chart.js');
chartjs.Chart.register(
     chartjs.whatever...
);
var myChart = new chartjs.Chart(ctx, {...});

Essentially mixing the way shown on how to tree shake with ESModule imports and CommonJS.

My question is really more or less is there something that I did incorrectly during setup such that the code provided in the documentation doesn't actually work? Or is it simply that the documentation may be out of date?

As for my setup I simply just installed the most recent version of Chart.js (currently 3.5.1) from npm.

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda