Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Chart.js cannot acquire a context from the item you passed

ErrorFailed to create chart: can't acquire context from the given item

Short answer

Chart.js needs a real canvas element. The message appears when the item you pass is null (wrong id, wrong case, or the element is not in the page yet) or when the code runs in Node.js, which has no canvas. Look the element up after it exists, or give Node.js a canvas package.

  • Applies to Chart.js 4, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Log what you pass to the Chart constructor, right before the call. null or undefined means the lookup failed; an object that is not a canvas element means a different mistake.

    const el = document.getElementById('acquisitions');
    console.log(el, el && el.tagName);
  2. 2Match what you see, then jump to its fix

    1. 1console.log prints nullThe canvas lookup returns nullFix
    2. 2The same call works in the browser but fails in a script, a test runner or server-side renderingThe code runs in Node.js, where there is no canvasFix

What does the error look like?

  • Failed to create chart: can't acquire context from the given item
  • The same chart code works in one page and fails in another

Cause 1 of 2

The canvas lookup returns null

1Check

document.getElementById returns null when no element has that id. The id is case-sensitive, and an element that has been created but not yet added to the document is not searched. If your script sits above the canvas in the HTML, the canvas does not exist yet when it runs.

console.log(document.getElementById('acquisitions'));
console.log(document.querySelectorAll('canvas').length);

2Fix

Match the id exactly, make sure the canvas is in the document, and run the script after it, for example with defer or at the bottom of the body. Then pass the element to Chart.

<canvas id="acquisitions"></canvas>
<script defer src="/chart-setup.js"></script>

// chart-setup.js
new Chart(document.getElementById('acquisitions'), config);

3Verify

The console no longer prints null, and the chart draws.

console.log(document.getElementById('acquisitions') instanceof HTMLCanvasElement);

Confirmed by developer.mozilla.org

Did this fix it?

Cause 2 of 2

The code runs in Node.js, where there is no canvas

1Check

Chart.js draws on a canvas, and Node.js has no DOM. Its Node guide says you can use Chart.js there with a canvas package such as node-canvas or skia-canvas.

node -p "typeof document"

2Fix

Install one of those packages and give Chart a canvas from it, as the Chart.js Node.js page shows with skia-canvas. For browser-only code, keep Chart.js out of server-side paths.

npm install chart.js skia-canvas

3Verify

Run the script and confirm it creates the chart without the context error.

node render-chart.js

Confirmed by chartjs.org

Did this fix it?

How do I stop it coming back?

  • Give each canvas a unique id and keep it in one place, so the id in HTML and JavaScript cannot drift apart.
  • Create the chart only after you have confirmed the element exists.
  • Pass the canvas element itself; the Chart.js usage guide creates charts that way.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

ng serve says it cannot determine the project or target

Cannot determine project for command.

The Angular CLI looked through angular.json for a project that defines the target you asked for (serve, build, test) and found none, or found several and could not pick one.

3 causes, each with a fix

Back end

ng serve cannot find the dev-server builder package

Could not find the implementation for builder @angular/build:dev-server

The builder named in angular.json (for example @angular-devkit/build-angular:dev-server) is looked up as an npm package starting from the workspace root.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.