Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect file changes in node.js via watchFile

I want to detect changes for a file, if the file changes, I will use child_process to execute a scp command to copy the file to a server.I looked up node.js documentation, the fs.watchFile function seems do what I want to do, but when I tried it, somehow it just doesn't work as I expected. The following code were used:

var fs = require('fs');                                                                        

console.log("Watching .bash_profile");

fs.watchFile('/home/test/.bash_profile', function(curr,prev) {
    console.log("current mtime: " +curr.mtime);
    console.log("previous mtime: "+prev.mtime);
    if (curr.mtime == prev.mtime) {
        console.log("mtime equal");
    } else {
        console.log("mtime not equal");
    }   
});

With above code, if I access the watched file, the callback function get execute, it will output the same mtime, and always output "mtime not equal" (I only accessing the file). Outputs:

Watching .bash_profile
current mtime: Mon Sep 27 2010 18:41:27 GMT+0100 (BST)
previous mtime: Mon Sep 27 2010 18:41:27 GMT+0100 (BST)
mtime not equal

Anybody know why the if statement failed(also tried using === identify check, but still get the same output) when the two mtime are the same?

like image 283
vito huang Avatar asked Sep 27 '10 17:09

vito huang


People also ask

How do you view file changes in node JS?

To watch any changes that are made to the file system, we can use the fs module provided by node. js that provides a solution here. For monitoring a file of any modification, we can either use the fs. watch() or fs.

How can you watch a file for changes and log the filename after each change?

You can maybe use the fs. appendFile(); function so that when something is being written into the file you emit an event to something else that "logs" your new data that is being written. This will allow you to watch a file for changes.

How do you investigate memory leaks in node JS?

A quick way to fix Node. js memory leaks in the short term is to restart the app. Make sure to do this first and then dedicate the time to seek out the root cause of the memory leak.

How do I view a JavaScript file?

JavaScript code is written in plain text, so you can use any popular file readers (Notepad and TextPad) or word processors (Microsoft Word or Apple Pages) to open JavaScript files. You only need to right-click on the JavaScript file and select the Open With..


1 Answers

If mtime properties are Date objects, then these can never be equal. In JavaScript two separate objects are equal ONLY if they are actually the same object (variables are pointing to the same memory instance)

obj1 = new Date(2010,09,27);
obj2 = new Date(2010,09,27);
obj3 = obj1; // Objects are passed BY REFERENCE!

obj1 != obj2; // true, different object instances
obj1 == obj3; // true, two variable pointers are set for the same object
obj2 != obj3; // true, different object instances

To check if these two date values are the same, use

curr.mtime.getTime() == prev.mtime.getTime();

(I'm not actually sure if this is the case since I didn't check if watchFile outputs Date objects or strings but it definitely seems this way from your description)

like image 85
Andris Avatar answered Oct 11 '22 14:10

Andris