Getting error "ReferenceError: Image is not defined" on NodeJS
mhcgolds opened this issue · 1 comments
Hello,
I'm trying to use this lib for the first time in a simple NodeJS app. And when I call ImageTracer.imageToSVG(url, callback);
, I got the error:
imagetracerjs\imagetracer_v1.2.5.js:1120
var img = new Image();
^ReferenceError: Image is not defined
at ImageTracer.loadImage (C:\Users...\node_modules\imagetracerjs\imagetracer_v1.2.5.js:1120:13)
at ImageTracer.imageToSVG (C:\Users...\node_modules\imagetracerjs\imagetracer_v1.2.5.js:56:9)
at getLogoImage (C:...\test.js:77:15)
at Request._callback (C:\U...\test.js:54:5)
at Request.self.callback (C:...\node_modules\request\request.js:185:22)
at Request.emit (events.js:198:13)
at Request. (C:...\node_modules\request\request.js:1161:10)
at Request.emit (events.js:198:13)
at IncomingMessage. (C:...\node_modules\request\request.js:1083:12)
at Object.onceWrapper (events.js:286:20)
This is how I'm including it: ImageTracer = require('imagetracerjs');
and the url
is valid. Node version is: 10.16.3
Any advice ?
Thank you
Hi,
The problem is that Node.js has no built in Image, Canvas and similar objects. It's mentioned in the Readme, but it's probably not that clear that imageToSVG()
and imageToTracedata()
don't work with Node.js directly.
The good news is that there are many solutions! The easiest is problably reading the nodecli example, where an external PNG.js is used to load an image file to an ImageData object and then calling imagedataToSVG()
.
You can also search the web for "Node.js canvas" to find modules which might support Image and Canvas objects.