Maaz Zindani

Loading the index…

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

Chart.js on Node.js fails because the canvas module is missing

ErrorError: 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.

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

Fix it fast

  1. 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-canvas
  2. 2Match what you see, then jump to its fix

    1. 1npm ls prints (empty) and the stack trace points at your server-side chart codeNo canvas package is installedFix
    2. 2npm install canvas fails, or the module is in package.json but the load still failscanvas is listed, but it did not install on this Node.js or platformFix

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.json

2Fix

Install node-canvas in the same project that runs the code.

npm install canvas

3Verify

The package is listed and the script runs.

npm ls canvas
node render-chart.mjs

Confirmed by chartjs.org

Did this fix it?

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 -sm

2Fix

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-canvas

3Verify

Render a chart to a file and open it.

node render-chart.mjs

Confirmed by raw.githubusercontent.com

Did this fix it?

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 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.