fs.watch to watch a file for changes. What will the output be when the file is modified?import { watch } from 'fs'; watch('example.txt', (eventType, filename) => { if (filename) { console.log(`${filename} file Changed: ${eventType}`); } });
The fs.watch function watches the file and triggers the callback with eventType 'change' when the file content changes. It does not throw an error and does not ignore file changes.
import { watch } from 'fs'; watch('log.txt', (event, file) => { console.log(`${file} was ${event}`); });
Option D correctly uses braces and a semicolon inside the arrow function body. Option D misses the closing parenthesis and semicolon, causing a syntax error.
import { watch } from 'fs'; watch('data.json', (eventType) => { if (eventType === 'change') { console.log('File changed'); } });
fs.watch can miss some changes especially if they happen quickly or in bursts. This is a known limitation of the underlying OS file watching APIs.
import { watch } from 'fs'; let count = 0; watch('notes.txt', (eventType, filename) => { if (eventType === 'change') { count++; console.log(`${filename} changed`); } });
fs.watch may combine multiple rapid changes into fewer events, so the callback might be called fewer than the number of actual changes.
fs.watch and fs.watchFile in Node.js.fs.watch relies on native OS events, making it more efficient but sometimes inconsistent. fs.watchFile uses polling, which is consistent but less efficient.