I am reading a book called "Node.js the right way."
one example of code in a book is to look at the text.txt file for a change.
The node.js code is as follows:
const fs = require('fs');
var filename = process.argv[2];
console.log(filename);
var count = 0;
if (!filename) {
throw Error("A file to watch must be specified!");
} else {
console.log(filename);
}
fs.watch(filename, function() {
count = count + 1;
console.log("File 'text.txt' has been changed ("+count+") times!");
});
console.log("Now watching " + filename + " for changes...");
The book says that the terminal command should be like this:
$ node --harmony watcher.js text.txt
However, this fails and gives the following error:
fs.js:1237
throw errnoException(err, 'watch');
^
Error: watch ENOENT
at exports._errnoException (util.js:837:11)
at FSWatcher.start (fs.js:1237:11)
at Object.fs.watch (fs.js:1263:11)
at Object.<anonymous> (/home/ubuntu/workspace/watcher.js:12:4)
at Module._compile (module.js:434:26)
at Object.Module._extensions..js (module.js:452:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Function.Module.runMain (module.js:475:10)
at startup (node.js:117:18)
If I did not type the full path to the target file, like this:
$ node --harmony watcher.js /home/ubuntu/workspace/text.txt
Which one is correct? and why does he identify the file "watcher.js" and not the text.txt file, although both are in the same directory? How can I overcome this and just type "text.txt" on the command line?
source
share