Chartjs-ChartJS and require: Chart is undefined

1đź‘Ť

If you are using v2.0 of Chart.js make sure you get a latest version (it worked for me with 2.0.0-beta2, but not with alpha). Also, since Chart.js depends on moment.js you’ll need to include that as well.


With older versions of Chart.js v2.0 (say alpha), you’ll have to load Chart.js as a legacy script and use the global variable (or noConflict()).


The dependency on moment.js is not very obvious (related issue – https://github.com/nnnick/Chart.js/issues/2110). Here’s what you get on the console, when you load Chart.js as a normal script file.

enter image description here

When Chart.js is loaded by require.js, this dependency causes a request for moment.js.

0đź‘Ť

For those also sent here by google, I had to shim the file to export the correct variable:

requirejs.config({
    baseUrl: 'scripts',
    paths: {
        ...
        'chartjs': 'vendor/Chart'
    },
    shim: { 
        'chartjs': {
            exports: 'Chart'
        }
    },
});

Leave a comment