question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ReferenceError: HTMLCanvasElement is not defined

See original GitHub issue

Trying to apply this library to code ported from the front end:

const ChartjsNode = require('chartjs-node')

let chart = new ChartjsNode(300, 200)

let result = require('./data.json')

let data = {
  labels: result.map(d => d.date[ 2 ] + '-' + d.date[ 1 ]),
  datasets: [
    {
      label: 'x',
      backgroundColor: 'rgba(255, 99, 132, 0.7)',
      borderColor: 'rgba(255, 99, 132, 0.7)',
      data: result.map(d => d.x)
    },
    {
      label: 'y',
      backgroundColor: 'rgba(54, 162, 235, 0.7)',
      borderColor: 'rgba(54, 162, 235, 0.7)',
      data: result.map(d => d.y)
    },
    {
      label: 'z',
      backgroundColor: 'rgba(255, 206, 86, 0.7)',
      borderColor: 'rgba(255, 206, 86, 0.7)',
      data: result.map(d => d.z)
    }
  ]
}

let options = {
  fill: false,
  legend: {
    position: 'bottom'
  },
  elements: {
    line: {
      tension: 0,
      fill: false
    }
  }
}

chart.drawChart({ type: 'line', data: data, options: options })
  .then(() => {
    chart.writeImageToFile('image/png', './chart.png')
    chart.destroy()
  })
  .catch(error => console.log(error))

This results in ReferenceError: HTMLCanvasElement is not defined.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
sedouardcommented, Dec 11, 2016

Published an updated version chartjs-node@1.1.1 with the updated peerDependency. Originally I had chart.js as a peerDependency because I wanted to give some flexibility on the version of chart.js you could use with this package 😕.

We’ll see if we can figure out what’s going on with the newer versions of chart.js.

0reactions
sedouardcommented, Dec 11, 2016

Not sure whats different in chart.js@2.40 vs chart.js@2.30. To be honest that’s just the version I tested against and set CI up for. I’ll change the peerDependencies to use chart.js@2.3.*. So that the npm install indicates an unmet dependency.

Won’t have time to see what the deal is with 2.4.0. But I’ll open another issue to support it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

In Next.js Error [ReferenceError: HTMLCanvasElement is not ...
js library like the code above. But when I run the code, I get the following error: ReferenceError: HTMLCanvasElement is not defined. error ......
Read more >
HTMLCanvasElement.toDataURL() - Web APIs | MDN
The HTMLCanvasElement.toDataURL() method returns a data URL containing a representation of the image in the format specified by the type ...
Read more >
How to test HTML5 canvas with jest? - Yonatan Kra
How to solve ReferenceError: Path2D is not defined. This error happens because Jest runs on JSDom and not in a real browser. Luckily,...
Read more >
jest-canvas-mock - npm
Mock Strategy. This mock strategy implements all the canvas functions and actually verifies the parameters. If a known condition would cause ...
Read more >
Writing Files with Node.js and JSDOM | by Danny Lee - Medium
Uncaught ReferenceError: require is not defined at index.js:1 ... /node_modules/jsdom/lib/jsdom/living/nodes/HTMLCanvasElement-impl.js:42:5)
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found