'How to append to second last line in a file in NodeJs

I want to append to second last line of a file using NodeJs!

I hope I am making sense.

Thanks in advance

My current code which appends to end of the file:

fs.appendFile('data.txt', 'something', function (err) {
  if (err) throw err;
  console.log('Saved!');
});


Sources

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

Source: Stack Overflow

Solution Source