'How to backup a file when a file got edited using nodejs fs module?

I am a beginner to nodejs please help me find this solution?

I need code that looks at a file and makes a backup of the file whenever it is changed and saved to the disk.

  1. have to create backup in separate folder.
  2. filenofoundexception if no particular file is not exists
  3. changes are watched and saved immediately and console it
    const fs = require('fs');
    
    fs.watchFile("sample.txt", {
        persistent: true,
        interval: 2000,
    }, (curr, prev)=>{
        fs.writeFile('backupFile.text', prev)
    })

I tried got this error

node:internal/validators:223
    throw new ERR_INVALID_CALLBACK(callback);
    ^

TypeError [ERR_INVALID_CALLBACK]: Callback must be a function. Received undefined


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source