Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

212
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda