Search code examples
node.jsimagemagickwatermark

Error while using image-watermark in node.js


I am using image-watermark for adding watermark to a file on server side node.js and it is producing an error.

I tried installing modules image-watermark, imagemagick, and other dependencies. At first, I faced a problem similar to this. But, this problem was resolved by installing ImageMagick into my system and now that is resolved from this answer.

Now, I am getting an error in the following code:

const watermark = require('image-watermark');
const imagePath = path.resolve(__dirname,'./relative/path/to/image');
watermark.embedWatermarkWithCb(imagePath, { 'text': 'sample watermark' }, 
function (err) {
    if (!err)
      console.log('Succefully embeded watermark');
    else {
      //I am getting error here.
      console.log(err);
    }
});

The image file should be exported with a watermark. But, instead I am getting this error:

{ Error: Command failed:
    at ChildProcess.<anonymous>(...\node_modules\imagemagick\imagemagick.js:88:15)
    at emitTwo (events.js:131:20)
    at ChildProcess.emit (events.js:214:7)
    at maybeClose (internal/child_process.js:925:16)
    at Process.ChildProcess._handle.onexit 
(internal/child_process.js:209:5) timedOut: false, killed: false, code: 1, 
signal: null }

I used this answer to get to the root cause of the error. Without that I got Error: spawn ENOENT error only.


Solution

  • I prepare a basic example for you:

    download the code here

    Pre-requisite:

    install imagemagick in SO
    install ghostscript in SO (for pdf files)
    

    Ex. for mac:

    brew install imagemagick
    brew install ghostscript (for pdf files)
    

    The steps are the following:

    - You must enter the folder ./stackoverflow/56353310/
    
    - npm install
    
    - node index.js
    

    The output must be something like that:

    enter image description here

    any question please let me know.