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

To use the emptyDir function from the fs-extra module in Node.js, first we need to install it using npm:

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

After that, we can require it in our .js file as shown below:

const fs = require('fs-extra');
32 chars
2 lines

To empty the contents of a directory using fs-extra's emptyDir():

const directory = './path/to/dir';
 
async function emptyDirectory() {
  try {
    await fs.emptyDir(directory);
    console.log(`${directory} successfully emptied!`);
  } catch (err) {
    console.error(err);
  }
}
 
emptyDirectory();
236 chars
13 lines

The emptyDir function empty the entire directory which means it removes all the files and directories inside it. It will also create the directory if it doesn't exist, so be careful while using it.

gistlibby LogSnag