Skip to main content

Node.js Setup

Konva can be used in Node.js environments for server-side rendering, image processing, and canvas operations. This guide will help you set up Konva in your Node.js project.

Konva Version 10+​

Konva v10+ dropped default support for Node.js environment. You now need to explicitly import a canvas backend.

Installation​

Konva v10+ offers three backend options for Node.js:

node-canvas Backend:

npm install konva canvas

Skia Backend (Better Performance):

npm install konva skia-canvas

napi-rs Backend (prebuilt binaries, no system libraries), since Konva 10.10:

npm install konva @napi-rs/canvas

Usage​

Import Konva and the backend module of your library. Leave out the stage container: there is no DOM in Node.js.

import Konva from 'konva';
import 'konva/canvas-backend'; // or 'konva/skia-backend', or 'konva/napi-backend'

// Create a stage
const stage = new Konva.Stage({
width: 800,
height: 600,
});

// ... the rest of your konva code

// Export as data URL
const dataURL = stage.toDataURL();

A backend module also sets the global DOMMatrix and Path2D of its library, so code like new Path2D() in a clipFunc works as it does in a browser.

Custom Backend​

Since Konva 10.10, Konva.setCanvasBackend() takes a canvas library directly. Use it to configure the library yourself, or to leave the global DOMMatrix and Path2D alone, for example in a package that embeds Konva next to an app that uses another canvas library:

import Konva from 'konva';
import { Canvas, Image, Path2D } from '@napi-rs/canvas';

Konva.setCanvasBackend({
name: 'my-canvas',
createCanvas: () => new Canvas(300, 150),
createImage: () => new Image(), // optional, to load images
createPath2D: (path) => new Path2D(path), // optional, for Konva.Util.createPath2D()
});

Konva.getCanvasBackend() returns the name of the active backend, or null before one is set. Set the backend before you create stages: stages and nodes created earlier keep the canvases of the old library.

Konva Version ≤ 9 (Legacy)​

For older versions of Konva, the setup was simpler:

Installation​

npm install konva

Setup​

const Konva = require('konva');

// Create a stage
const stage = new Konva.Stage({
container: 'container', // This will be ignored in Node.js
width: 800,
height: 600
});

// ... the rest of your konva code

// Export as data URL
const dataURL = stage.toDataURL();

Server-Side Rendering Considerations​

When using Konva in Node.js, keep in mind:

  1. No DOM: Konva doesn't require a DOM, making it perfect for server-side rendering
  2. Canvas Export: Use stage.toDataURL() to export your canvas as an image
  3. Memory Management: Be mindful of memory usage when processing multiple canvases
  4. Performance: Konva performs well in Node.js environments for batch operations
  5. SSR Frameworks: For Next.js and other SSR frameworks, consider using client-side only rendering for canvas content

Common Use Cases​

  • Image Generation: Create dynamic images for emails, reports, or social media
  • Chart Generation: Generate charts and graphs server-side
  • Document Processing: Add graphics to PDFs or other documents
  • Batch Processing: Process multiple images or graphics in parallel