Getting internal server error when trying to use canvas lib to create img element

Code:

var Canvas = require(‘canvas’);
var Image = Canvas.Image;
const img = new Image();
img.src=‘someImageURL’;

Error:
Internal Server Error
TypeError: s is not a constructor
at worker.js:6118:2449
at worker.js:6118:2719 at line 6117, col 2447

Is this on a Cloudflare Worker? Or in the browser?

If it’s in the browser then it’s in no way a Cloudflare problem.

If it’s a Cloudflare Worker it’s likely that the package canvas isn’t supported on Cloudflare Workers (Workers don’t run Node.js and so are missing some common Node.js modules).

This is on Cloudflare Worker. Any way to do what I am trying here (creating an img element) in Cloudflare worker

Not really. Cloudflare Workers don’t have a DOM to create an img in so there’s really no point anyway.

In the future I’d recommend you ask these type of questions on the Cloudflare Developers Discord or StackOverflow, this Community is mostly for Cloudflare support and Cloudflare-specific questions (not so much coding help).

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.