最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Uncaught ReferenceError: Chart is not defined - Stack Overflow

programmeradmin4浏览0评论

I've installed chart.js using npm by : npm install chart.js --save-dev, in "resources/assets/js/bootstrap.js"
I refer to it by: require('chart.js');
Then in my console npm run dev and finally it's successfully piled in "public/js/app.js", however when I try to use it in my view as follow

<script src="/js/app.js"></script><script>
var ctx = document.getElementById("myChart");
var myChart = new Chart(ctx, {
......... </script>

the browser returns

Uncaught ReferenceError: Chart is not defined.

How e it's declared in app.js and can't refer to it ?
Thanks in advance.

I've installed chart.js using npm by : npm install chart.js --save-dev, in "resources/assets/js/bootstrap.js"
I refer to it by: require('chart.js');
Then in my console npm run dev and finally it's successfully piled in "public/js/app.js", however when I try to use it in my view as follow

<script src="/js/app.js"></script><script>
var ctx = document.getElementById("myChart");
var myChart = new Chart(ctx, {
......... </script>

the browser returns

Uncaught ReferenceError: Chart is not defined.

How e it's declared in app.js and can't refer to it ?
Thanks in advance.

Share Improve this question edited Mar 29, 2017 at 18:09 Martin 22.9k13 gold badges77 silver badges144 bronze badges asked Mar 29, 2017 at 15:24 Yamen AshrafYamen Ashraf 2,9602 gold badges25 silver badges28 bronze badges 1
  • Maybe you're using strict "use strict" mode? – DevK Commented Mar 29, 2017 at 15:26
Add a ment  | 

2 Answers 2

Reset to default 2

If you're using es6 you might need to change the way you require it.

from the docs: http://www.chartjs/docs/

// Using CommonJS
var Chart = require('chart.js')
var myChart = new Chart({...})

// ES6
import Chart from 'chart.js'
let myChart = new Chart({...})

// Using requirejs
require(['path/to/Chartjs'], function(Chart){
var myChart = new Chart({...})
})

If you look in app.js, is it wrapped in a function? It sounds like it's not part of the global namespace, despite being present in app.js.

发布评论

评论列表(0)

  1. 暂无评论