0

How to add custom text to the end of .csv file in NodeJS? Currently, I parse csv file and do some validation against predefined parameters (separator, columns number etc.) and after that I have to add custom line at the end of file.

Example:

Col1 Col2 Col3 Col4
Row1 Row1 Row1 Row1
Row2 Row2 Row2 Row2
Row3 Row3 Row3 Row3

And at the end, there should be custom line with line ending character.

Ex:

this is the end of file\n

1 Answer 1

2

You can simply use appendFile:

Asynchronously:

const fs = require('fs');

fs.appendFile('yourfile.csv', 'this is the end of file\n', function (err) {
  if (err) throw err;
  console.log('Saved!');
});

Synchronously:

const fs = require('fs');

fs.appendFileSync('yourfile.csv', 'this is the end of file\n');
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.