I'm trying to write a node.js script that watches for changes in a directory of files, and then prints the files that are changed. How can I modify this script so that it watches a directory (instead of an individual file), and prints the names of the files in the directory as they are changed?
var fs = require('fs'),
sys = require('sys');
var file = '/home/anderson/Desktop/fractal.png'; //this watches a file, but I want to watch a directory instead
fs.watchFile(file, function(curr, prev) {
alert("File was modified."); //is there some way to print the names of the files in the directory as they are modified?
});
Why not just use the old
fs.watch
? Its pretty straightforward.For more info and details about the options param, see Node fs Docs
try hound:
It will execute once file was change
Try Chokidar:
Chokidar solves some of the crossplatform issues with watching files using just fs.