merge-images: TypeError: options.Canvas is not a constructor

I am running node, and am attempting to merge two images with this code:

const mergeImages = require('merge-images');
const Canvas = require('canvas');

mergeImages(["img1.png", "img2.png"],
     {
        Canvas: Canvas
      })
      .then(b64 => function()
      {
      //Do stuff
      });

However, the above gives an error:

(node:19271) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): TypeError: options.Canvas is not a constructor

I saw this, but after changing my code to const Canvas = require('canvas').Canvas;, like was suggested, I got another error: TypeError: Image is not a constructor Like was suggested in the issue above, I tried downgrading to canvas 1.6.13, but that resulted in: ReferenceError: window is not defined

Not sure if it’s me, merge-images. or canvas that’s causing the issue.

About this issue

  • Original URL
  • State: open
  • Created 6 years ago
  • Reactions: 4
  • Comments: 33 (9 by maintainers)

Most upvoted comments

It should help:

const {Canvas,Image}=require('canvas');
Canvas.Image=Image;

@lukechilds hello , everyone I have faced this issue and saw the code , the solution is you have to 1- require const Canvas = require('canvas'); 2- pass the Canvas as option like {Canvas: Canvas} 3- go to a file called index.umd.js into the node_modules/merge-images/… and make this change new options.Canvas.Canvas() at line 25 instead new options.Canvas() thanks,

No not yet, for now you’ll need to either use node-canvas v1 or submit a PR to make merge-images work with node-canvas v2.

If you find the right candidate, and when the updates are done, I will remove my fork.

No problem, thanks for weighing in @MarcGodard totally understand.

Congrats on the little one! 👶🎉

It requires some changes to this library to be compatible with node-canvas v2. I’m very busy with paid work at the moment so don’t have time to work in it right now.

Using node-canvas v1 should work for now.