Chart.js cannot acquire a context from the item you passed
Failed to create chart: can't acquire context from the given itemShort 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.
Fix it fast
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);2Match what you see, then jump to its fix
What does the error look like?
Failed to create chart: can't acquire context from the given itemThe 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
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-canvas3Verify
Run the script and confirm it creates the chart without the context error.
node render-chart.jsConfirmed by chartjs.org
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 fixBack end
ng fails with Cannot find module @angular/cli/bin/ng
Cannot find module '@angular/cli/bin/ng'
The ng command is the entry point to the Angular CLI package.
2 causes, each with a fixBack end
Angular app shows 'Cannot GET /' when served by a Node.js server
Cannot GET /dashboard
Cannot GET / is Express saying no route or file matches the request.
3 causes, each with a fixBack 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 fixBack end
Angular build says it cannot resolve '@angular/localize/init'
Can't resolve '@angular/localize/init' in '/usr/src/app/src'
The build imports @angular/localize, and that package is not in node_modules.
2 causes, each with a fixBack end
The command npm run build -- --prod exited with code 1 (Angular)
The command "npm run build -- --prod" exited with code 1
Exit code 1 only says the build failed; the real message is above it.
2 causes, each with a fix