how to use the rm function from the fs-extra library in typescript

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

// example usage of fs-extra's rm function
fs.rm('/path/to/delete', { recursive: true }, (err) => {
  if (err) {
    console.error(err);
  } else {
    console.log('File or directory removed!');
  }
});
231 chars
11 lines

In the code above, we import the fs-extra library and then call its rm function by passing the file or directory path as a string and an options object as its second argument.

The options object has a recursive flag which, when set to true, enables the function to remove directories with their subdirectories and files. The third argument is a callback function that will be called after the operation is completed. If an error occurs during the removal process, the callback function receives the error object via its first parameter.

Note: Make sure to have the proper permissions to delete the file or directory.

related categories

gistlibby LogSnag