3

I am trying to put a watermark on an image, using image-watermark, nodejs.

const express = require('express')    
const app = express();    
var path = require('path');    
var watermark = require('image-watermark');       
app.use(express.static(path.join(__dirname, 'public')));

var options = {
    'text' : 'sample watermark', 
    'resize' : '200%'
};

app.get('/', function (req, res) {
    var fs = require('fs');
    if (fs.existsSync('./public/files/pg363.jpg')) {
        // Do something
        watermark.embedWatermark('./public/files/pg363.jpg', options);
        res.send('Hello world');
    }else{
        res.json({"filesexist":"no"});
    }

});


app.listen(3000,function(){
    console.log('App running on server 3000');
})

This is giving me error:

    events.js:163
      throw er; // Unhandled 'error' event
      ^

Error: spawn identify ENOENT
    at exports._errnoException (util.js:1050:11)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:193:32)
    at onErrorNT (internal/child_process.js:367:16)
    at _combinedTickCallback (internal/process/next_tick.js:80:11)
    at process._tickCallback (internal/process/next_tick.js:104:9)

is this error related to filepath. my directory structure is pdf-watermark(home directory)-> public-> files-> pg363.jpg

Please help.

raju
  • 6,448
  • 24
  • 80
  • 163

1 Answers1

0

ENOENT means that node can't open the file for some reason, which could be caused by a few things.

Firstly, you should specify the full path to your image file, using path.resolve and __dirname:

const imagePath = path.resolve(__dirname, 'public/files/pg363.jpg');
if (fs.existsSync(imagePath)) {
    watermark.embedWatermark(imagePath, options);
    // ...
}

Secondly, you should also make sure that node is running as a user with appropriate read/write permissions to update the image file.

Phil Booth
  • 4,853
  • 1
  • 33
  • 35