node.js file watcher

File watcher to detect if a file has been changed.

//usage: node watcher.js filename &
var fs = require('fs');
var file = process.argv[2];
if(file) {
	try {
		fs.watch(file, function() {
			console.log(file, "just changed.");
		});	
		console.log("Start watching", file ,"for changes...");
	} catch (e) {
		if (e.errno === 'ENOENT') {
			console.log("Error no such file", file);
		} else {
			console.log(e);
		}
	}
} else {
	console.log("Please specify the file you want to watch.");
	console.log(">$ node watcher.js filename &")
}

Search within Codexpedia

Custom Search

Search the entire web

Custom Search