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.

getting "window is not defined" error with nodejs.

See original GitHub issue

Getting “ReferenceError: window is not defined” when using with nodejs. Below given is my approach.

var mergeImages = require("merge-images");

mergeImages([
 { src: image1.buffer },
 { src: image2.buffer }
]).then(b64 => {
 console.log(b64);
});

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
RedSparr0wcommented, Mar 21, 2020

You can solve this by using:

const mergeImages = require('merge-images');
const { Canvas, Image } = require('canvas');
 
mergeImages(['./body.png', './eyes.png', './mouth.png'], {
  Canvas: Canvas,
  Image: Image
}).then(b64 => ...);

Info from the readme here: https://www.npmjs.com/package/merge-images#nodejs-usage

1reaction
kapil-joshicommented, Sep 11, 2019

Actually, i changed my code as below:


const imgMerge = require("merge-images"); 
const nodeCanvas = require('canvas').Canvas;

const img1 = 'data:image/jpeg;base64,/9j/4AAQSkZJRgAB........';
const img2 = 'data:image/jpeg;base64,/9j/4AAQSkZJRgAB........';
const img3 = 'data:image/jpeg;base64,/9j/4AAQSkZJRgAB........';

return imgMerge([{ src: new Image(img1), x: 0, y: 0 },
          { src: new Image(img2), x: 100, y: 0 },
          { src: new Image(img3), x: 500, y: 0 }], {Canvas: nodeCanvas})
          .then(b64 => agent.add(new Image(b64)));

But getting an error:

Image is not a constructor at /srv/node_modules/merge-images/dist/index.umd.js:39:13 at new Promise (<anonymous>) at /srv/node_modules/merge-images/dist/index.umd.js:32:54 at Array.map (<anonymous>) at /srv/node_modules/merge-images/dist/index.umd.js:32:23 at new Promise (<anonymous>) at mergeImages (/srv/node_modules/merge-images/dist/index.umd.js:21:9)

Some one please help asap.

Thanks, Kapil

Read more comments on GitHub >

github_iconTop Results From Across the Web

ReferenceError : window is not defined at object. <anonymous ...
window object is present only in the context of browser. When running application on nodejs no window object is available.
Read more >
How To Solve ReferenceError window is not defined in ...
js application and still receive this error, it is because the window object does not actually exist in a Node.js environment. To resolve...
Read more >
[Solved] ReferenceError : window is not defined - ItsJavaScript
The ReferenceError : window is not defined error mainly occurs if you are using the window object in Node.js, React.js, Next.js.
Read more >
referenceerror: window is not defined, how to solve
Here's how to fix the “referenceerror: window is not defined” error that you might have in Node.js or with a tool like Next.js....
Read more >
ReferenceError: window is not defined in JavaScript
The "ReferenceError: window is not defined" error occurs for multiple reasons: ... The window object represents a window containing a DOM document and...
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