Search code examples
javascriptnode.jsfilesystemwatcher

Watching folder for change in Node.js


Im looking for a way to notice changes in a directory in Node.js. I read THIS and was thinking thats exactly what i want. But chokidar spawns to many events.

My code looks like this:

chokidar = require('chokidar');

var watcher = chokidar.watch('./Update', {ignored: /[\/\\]\./, persistent: true});
var count=0;
watcher
    .on('add', function(path) 
    {
        count++;
        console.log('File ' + path + ' has been added' + count);    
    })

    .on('error', function(error) {console.error('Error happened', error);})

When i run it and move a file in the Update directory i get the following output:

File Update\Neues Textdokument.txt has been added1

File Update\Neues Textdokument.txt has been added2

File Update\Neues Textdokument.txt has been added3

In the future i want to replace the console output with code that actualy does something, so i cant have moving one file spawning 3 events. Am I using the libary wrong? Or is the libary just not suited for what I want to do? If so what libary should i use instead?


Solution

  • try the following patch:

    https://github.com/paulmillr/chokidar/pull/124

    as stated in

    https://github.com/paulmillr/chokidar/issues/104