Chart.js on Node.js fails because the canvas module is missing
Error: Cannot find module 'canvas'Short answer
Chart.js draws on a canvas, and Node.js has none. To render charts on a server you add a canvas package such as node-canvas (npm package canvas) or skia-canvas. The error means that package is not installed, or its install did not complete on your Node.js version or platform.
Fix it fast
1Find out which cause you have
Ask npm whether a canvas package is installed in the project that runs the chart code.
npm ls canvas skia-canvas2Match what you see, then jump to its fix
What does the error look like?
Error: Cannot find module 'canvas'
Cause 1 of 2
No canvas package is installed
1Check
Chart.js itself ships no canvas. The Chart.js guide for Node.js says to use a canvas package for server-side output, and names node-canvas and skia-canvas.
grep -n -E '"(canvas|skia-canvas|chart.js)"' package.json2Fix
Install node-canvas in the same project that runs the code.
npm install canvas3Verify
The package is listed and the script runs.
npm ls canvas
node render-chart.mjsConfirmed by chartjs.org
Cause 2 of 2
canvas is listed, but it did not install on this Node.js or platform
1Check
Current node-canvas needs Node.js 22 or newer and ships prebuilt binaries only for certain systems (macOS, Linux and Windows on common CPU types).
node --version
uname -sm2Fix
Upgrade Node.js. If your platform has no prebuilt binary, the Chart.js guide names skia-canvas as the other supported package; its sample shows how to create the canvas from it.
npm install skia-canvas3Verify
Render a chart to a file and open it.
node render-chart.mjsConfirmed by raw.githubusercontent.com
How do I stop it coming back?
- Add the canvas package to dependencies, not only to your machine, so the server and CI install it.
- Run server-side chart rendering in a Node.js script or API route, separate from your browser code.
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