Complete the code to import the built-in module for watching files.
const fs = require('[1]');
The built-in Node.js module to watch files is fs. You import it using require('fs').
Complete the code to watch a file named 'example.txt' for changes.
fs.[1]('example.txt', (eventType, filename) => { if (filename) { console.log(`${filename} file changed`); } });
The fs.watch method watches for changes on a file or directory.
Fix the error in the callback parameter name to correctly detect the changed file.
fs.watch('data.json', (eventType, [1]) => { if (filename) { console.log(`${filename} was modified`); } });
The callback parameter for the filename is filename (all lowercase). It must match the variable used inside the function.
Fill both blanks to watch a directory named 'logs' and log the event type and filename.
fs.[1]('logs', ([2], filename) => { console.log(`Event: ${eventType}, File: ${filename}`); });
Use fs.watch to watch the directory. The first callback parameter is eventType which tells what happened.
Fill all three blanks to watch 'config.json', check if the event is 'rename', and log the filename.
fs.watch('config.json', ([1], [2]) => { if (eventType === [3]) { console.log(`File ${filename} was renamed.`); } });
The callback parameters are eventType and filename. To check if the file was renamed, compare eventType to the string 'rename'.