how to use the rmdir function from fs-extra in javascript

To use the rmdir function from the fs-extra package in Node.js, you first need to install the package using npm:

index.tsx
npm install fs-extra
21 chars
2 lines

Then, you can require the package and use its rmdir function to delete a directory and its contents:

index.tsx
const fs = require('fs-extra');

fs.rmdir('/path/to/directory', (err) => {
  if (err) {
    console.error(err);
  } else {
    console.log('Directory deleted!');
  }
});
170 chars
10 lines

In the above example, rmdir is passed the path to the directory to be deleted as its first parameter. The second parameter is a callback function that is called once the directory has been deleted or an error has occurred.

Note that the rmdir function will only delete an empty directory by default. To delete a directory and its contents, you can use the fs-extra package's remove function instead:

index.tsx
const fs = require('fs-extra');

fs.remove('/path/to/directory', (err) => {
  if (err) {
    console.error(err);
  } else {
    console.log('Directory and contents deleted!');
  }
});
184 chars
10 lines

The remove function takes the same parameters as rmdir, but will delete both files and directories within the specified directory.

gistlibby LogSnag