-4

I want to keep watching a folder (Folder Name Js) that contains some .js and other extension files also. Now i want to perform an action whenever .js file changed / update

TBH, i didn't tried anything. Although i can watch directory with fs.stat() and keep watching whenever the bytes change. But i only want to perform when .js files changed.

RKS
  • 1
  • 3

1 Answers1

0

You can just ignore changes made to non-.js files:

fs.watch("some_directory", (event, filename) => {
  if (!filename.endsWith(".js")) {
    return;
  }

  // Do what you have to do with the .js file.
});
Roj
  • 995
  • 1
  • 8
  • 22
  • Your Solution is working but whenever files changes with .js extension. The Callback function runs twice. – RKS Jul 31 '23 at 05:33
  • That shouldn’t be the case iirc. What do you do to the file when it runs twice? Are you sure that it’s not being written to twice, like with by an editor or something? – Roj Jul 31 '23 at 07:02
  • Yus ....I'm sure that that file is only written once. Even when i run > touch a.js The function runs twice ... Don't know What to Do – RKS Jul 31 '23 at 11:47