To watch a folder in Node.js we can use the built-in fs
module which provides a watch()
method that allows us to monitor a directory for changes. Here's an example of how to use fs.watch()
to watch for changes in a folder:
index.tsx258 chars11 lines
In this example, the watch()
method takes two arguments: the path to the folder we want to watch, and a callback that will be called when a change is detected in the folder.
The callback function receives two parameters: the eventType
which is a string indicating the type of event that occurred (e.g. 'rename' or 'change'), and filename
which is the name of the file that triggered the event.
We can use this method to monitor a directory for changes in real-time and take appropriate actions whenever a file is added, modified or deleted.
gistlibby LogSnag