Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

210
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!