Refer this link
Using this you can write to a specific position in a file.
Code snippet:
var position = 5;
var file_path = 'file.txt';
var new_text = 'abcde';
fs.readFile(file_path, function read(err, data) {
if (err) {
throw err;
}
var file_content = data.toString();
file_content = file_content.substring(position);
var file = fs.openSync(file_path,'r+');
var bufferedText = new Buffer(new_text+file_content);
fs.writeSync(file, bufferedText, 0, bufferedText.length, position);
fs.close(file);
});
The file.txt should be on the same path.
file.txt already contains this text: OldText
Output: The new text will be OldTeabcde
xt i.e, OldTeabcdext