question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Canvas node not working

See original GitHub issue

I have a code which it seems the same as your example page

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

const sendMerged = function(email) {
    mergeImages(['../public/logo.png', '../public/710fb.png'], {
      Canvas: Canvas
    })
    .then //....

But I get the error

TypeError: options.Canvas is not a constructor
    at /Users/xxx/GIT/projects/abc/node_modules/merge-images/dist/index.umd.js:25:32

If I console log Canvas I have the following

{ Canvas: { [Function: Canvas] _registerFont: [Function: _registerFont] },
  Context2d: [Function: CanvasRenderingContext2D],
  CanvasRenderingContext2D: [Function: CanvasRenderingContext2D],
  CanvasGradient: [Function: CanvasGradient],
  CanvasPattern: [Function: CanvasPattern],
  Image: { [Function: Image] MODE_IMAGE: 1, MODE_MIME: 2 },
  ImageData: [Function: ImageData],
  //...
}

So I tried to change the code above to

//...
const Canvas = require('canvas').Canvas; //This is against the canvas documentation example
//...

After this change it seems to be fine with the constructor but it fails the line after with

TypeError: Image is not a constructor
    at /Users/xxx/GIT/projects/abc/node_modules/merge-images/dist/index.umd.js:39:13

I wonder if it’s a bug with the new version of a dependency (I noticed you don’t pin them). Do your test run a new npm install? is this functionality tested? Thanks a lot

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:13 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
lukechildscommented, Mar 11, 2020

Thanks for your work @TobiTenno but I think merge-images should just be updated to work with canvas@2 rather than patch canvas@2 to expose a canvas@1 compatible API.

canvas@2 has been out for a while now so support for canvas@1 can be dropped.

1reaction
LinusUcommented, Nov 15, 2017

Sounds smart, we cannot guarantee that that api will be the final 2.x 👍

Read more comments on GitHub >

github_iconTop Results From Across the Web

error canvas.node · Issue #2039 - GitHub
I use macOS Monterey 12.4,I have been troubled in the same issue all day ,trying to find an solution here and there ,but...
Read more >
30 - Stack Overflow
I had the same problem. The issue was that the install script for node-canvas never got executed, which is why the build folder...
Read more >
How to fix an issue installing Node `canvas` on macOS
I wanted to use the Node.js canvas NPM package but running npm install canvas failed with messages like this:
Read more >
npm package canvas not working - Replit
As the title says I'm trying to use the canvas package. But when I run the code It gives me this: I expected...
Read more >
canvas node module and Google Cloud Functions [38478412]
Make sure you have the latest version of node.js and npm installed. npm ERR! If you do, this is most likely a problem...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found