how to use appendfilesync from fs-extra library in typescript

To use appendFileSync from fs-extra library in typescript, you will need to import the fs-extra module and use the appendFileSync method as shown in the code snippet below:

index.ts
import * as fs from 'fs-extra';

try {
  const filePath = 'file.txt'; // path to file
  const data = 'This is some new data to append.'; // data to add to file
  fs.appendFileSync(filePath, data); // append data to file
  console.log('Data added to file successfully.');
} catch (err) {
  console.error(err);
}
311 chars
11 lines

The appendFileSync method takes two parameters: the first parameter is the path to the file that you want to append data to and the second parameter is the data that you want to add to the file.

Note that appendFileSync blocks the execution until data is written to the file. If an error occurs, it will throw an exception. In the example above, we have used a try-catch block to handle any exceptions that may occur during the operation.

gistlibby LogSnag