chartjs-node-canvas is a Node.js library that enables server-side rendering of Chart.js charts into static image files (PNG, JPEG). It achieves this by utilizing the `canvas` package, which provides a Canvas API implementation compatible with Node.js environments, bypassing browser-specific rendering requirements. The current stable version is 5.0.0, which maintains compatibility with Chart.js v3.x and v4.x. While there isn't a strict, rapid release cadence, the project is actively maintained, with regular updates for dependency bumps and Node.js version support. Its primary differentiator is providing a reliable, performant way to generate Chart.js images outside of a browser, essential for tasks like generating reports, social media share images, or email attachments from server-side applications. It integrates directly with Chart.js as a peer dependency, requiring Chart.js itself to be installed separately, and allowing developers to manage its version.
npm install chartjs-node-canvasVerified import paths — ran on the pinned version, not inferred.
Demonstrates how to create an instance of `ChartJSNodeCanvas`, register necessary Chart.js components, define chart configuration, render it to a PNG buffer, and save it to a file on disk. This example shows a basic bar chart.
Ensure your `chart.js` dependency is `^3.0.0` or `^4.0.0`. Explicitly import and `Chart.register()` all necessary components (e.g., `CategoryScale`, `LinearScale`, `BarController`) from `chart.js` before rendering any charts.
Ensure `canvas` is correctly installed and built for your environment. If issues persist, try running `npm install canvas --build-from-source` or `npm rebuild` to ensure native modules are compiled correctly for your Node.js version.
This is intended behavior for server-side rendering. Do not rely on animation or client-side responsiveness in charts generated with this library. Ensure your chart configuration is static.
Use `chartJSNodeCanvas.registerFont('./path/to/your/font.ttf', { family: 'Your Font Name' });` before rendering charts. Then specify `font.family` in your Chart.js options.Ensure `canvas` is installed alongside `chartjs-node-canvas` (e.g., `npm install chartjs-node-canvas canvas chart.js`). If the issue persists, try `npm rebuild` or `npm install canvas --build-from-source`.
Import the specific chart components (e.g., `CategoryScale`, `LinearScale`, `BarController`) from `chart.js` and call `Chart.register()` with them before creating charts.
Run `npm install chartjs-node-canvas` to ensure the package is correctly installed.
Ensure the directory where you are attempting to save the image exists and that the user running the Node.js process has write permissions for that directory.