0

I need to watch a file in WAMP Server, so that when the file changes the javascript can run a function.

I have done some search and saw the library Node.js, but I am having a problem to use in wamp server. I ran the setup of Node.js to install in my computer. But when I call this in my webpage:

var fs = require('fs');                                                                        

            console.log('Watching watcher.txt');

            fs.watchFile('watcher.txt', 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');
                }   
            });

I get this error: "Uncaught ReferenceError: require is not defined".

Can someone help me? The Node.js in Wamp Server is doing a lot of confusion in my head.

BackSpace
  • 45
  • 6

2 Answers2

0

If you mean you want to run it on the server you'll need to install Node.js separately from WAMP on the same server, run them both and then point your Node.js script to a file in your WAMP directory.

Node.Js and PHP are both different ways to program back-end stuff for a website, so this usually means that a website runs on either Node or PHP but not both. I think in your situation (assuming you built your website in PHP) you would be better of creating a filewatcher in PHP.

Take a look at https://github.com/PHPGangsta/FileWatcher for some inspiration on how to do that.

Jos Vlaar
  • 11
  • 1
  • 4
0

I have found the solution. With that simple code and don't need to install any library in WAMP Server.

Solution: Is it possible to retrieve the last modified date of a file using Javascript?

Community
  • 1
  • 1
BackSpace
  • 45
  • 6