Observe file changes with node.js

Also you could use node-watch. Here's an easy example:

const watch = require('node-watch')

watch('README.md', function(event, filename) {
  console.log(filename, ' changed.')
})

Good news is that you can observe filechanges with Node's API.

This however doesn't give you access to the contents that has been written into the file. 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.

fs.watch(): Directly pasted from the docs

fs.watch('somedir', function (event, filename) {
    console.log('event is: ' + event);
    if (filename) {
        console.log('filename provided: ' + filename);
    } else {
        console.log('filename not provided');
    }
});

Read here about the fs.watch(); function

EDIT: You can use the function

fs.watchFile(); 

Read here about the fs.watchFile(); function

This will allow you to watch a file for changes. Ie. whenever it is accessed by some other processes of any kind.

Tags:

Node.Js